/', '', $business_one_page_text)) <= $business_one_page_length) { return $business_one_page_text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $business_one_page_text, $business_one_page_lines, PREG_SET_ORDER); $business_one_page_total_length = strlen($business_one_page_ending); $business_one_page_open_tags = array(); $business_one_page_truncate = ''; foreach ($business_one_page_lines as $business_one_page_line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($business_one_page_line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $business_one_page_line_matchings[1])) { // do nothing // if tag is a closing tag } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $business_one_page_line_matchings[1], $business_one_page_tag_matchings)) { // delete tag from $business_one_page_open_tags list $business_one_page_pos = array_search($business_one_page_tag_matchings[1], $business_one_page_open_tags); if ($business_one_page_pos !== false) { unset($business_one_page_open_tags[$business_one_page_pos]); } // if tag is an opening tag } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $business_one_page_line_matchings[1], $business_one_page_tag_matchings)) { // add tag to the beginning of $business_one_page_open_tags list array_unshift($business_one_page_open_tags, strtolower($business_one_page_tag_matchings[1])); } // add html-tag to $business_one_page_truncate'd text $business_one_page_truncate .= $business_one_page_line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $business_one_page_content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $business_one_page_line_matchings[2])); if ($business_one_page_total_length+$business_one_page_content_length> $business_one_page_length) { // the number of characters which are left $business_one_page_left = $business_one_page_length - $business_one_page_total_length; $business_one_page_entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|[0-9]{1,7};|[0-9a-f]{1,6};/i', $business_one_page_line_matchings[2], $business_one_page_entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($business_one_page_entities[0] as $business_one_page_entity) { if ($business_one_page_entity[1]+1-$business_one_page_entities_length <= $business_one_page_left) { $business_one_page_left--; $business_one_page_entities_length += strlen($business_one_page_entity[0]); } else { // no more characters left break; } } } $business_one_page_truncate .= substr($business_one_page_line_matchings[2], 0, $business_one_page_left+$business_one_page_entities_length); // maximum lenght is reached, so get off the loop break; } else { $business_one_page_truncate .= $business_one_page_line_matchings[2]; $business_one_page_total_length += $business_one_page_content_length; } // if the maximum length is reached, get off the loop if($business_one_page_total_length>= $business_one_page_length) { break; } } } else { if (strlen($business_one_page_text) <= $business_one_page_length) { return $business_one_page_text; } else { $business_one_page_truncate = substr($business_one_page_text, 0, $business_one_page_length - strlen($business_one_page_ending)); } } // if the words shouldn't be cut in the middle... if (!$business_one_page_exact) { // ...search the last occurance of a space... $business_one_page_spacepos = strrpos($business_one_page_truncate, ' '); if (isset($business_one_page_spacepos)) { // ...and cut the text in this position $business_one_page_truncate = substr($business_one_page_truncate, 0, $business_one_page_spacepos); } } // add the defined ending to the text $business_one_page_truncate .= $business_one_page_ending; if($business_one_page_considerHtml) { // close all unclosed html-tags foreach ($business_one_page_open_tags as $business_one_page_tag) { $business_one_page_truncate .= '' . $business_one_page_tag . '>'; } } return $business_one_page_truncate; } /** Filter for Body Class **/ function business_one_page_layout_class( $business_one_page_classes ){ global $post; if( is_404()){ $business_one_page_classes[] = ''; }elseif( !( is_active_sidebar( 'right-sidebar' )) || is_page_template( 'template-home.php' ) ) { $business_one_page_classes[] = 'full-width'; }elseif(is_singular()){ $business_one_page_post_class = get_post_meta( $post->ID, 'business_one_page_sidebar_layout', true ); if( $business_one_page_post_class == 'no-sidebar' ) $business_one_page_classes[] = 'full-width'; }else{ $business_one_page_classes[] = ''; } return $business_one_page_classes; } add_filter( 'body_class', 'business_one_page_layout_class' ); /** * Hook to move comment text field to the bottom in WP 4.4 * * @link http://www.wpbeginner.com/wp-tutorials/how-to-move-comment-text-field-to-bottom-in-wordpress-4-4/ */ function business_one_page_move_comment_field_to_bottom( $business_one_page_fields ) { $business_one_page_comment_field = $business_one_page_fields['comment']; unset( $business_one_page_fields['comment'] ); $business_one_page_fields['comment'] = $business_one_page_comment_field; return $business_one_page_fields; } add_filter( 'comment_form_fields', 'business_one_page_move_comment_field_to_bottom' ); /** * Callback function for Comment List * * * @link https://codex.wordpress.org/Function_Reference/wp_list_comments */ function business_one_page_theme_comment( $business_one_page_comment, $business_one_page_args, $business_one_page_depth ){ $GLOBALS['comment'] = $business_one_page_comment; extract($business_one_page_args, EXTR_SKIP); if ( 'div' == $business_one_page_args['style'] ) { $business_one_page_tag = 'div'; $business_one_page_add_below = 'comment'; } else { $business_one_page_tag = 'li'; $business_one_page_add_below = 'div-comment'; } ?> < id="comment-">