diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index ce876890be..110e2436f4 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -1863,21 +1863,39 @@ function wp_trim_excerpt($text) { $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); - $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); - $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); - if ( count($words) > $excerpt_length ) { - array_pop($words); - $text = implode(' ', $words); - $text = $text . $excerpt_more; - } else { - $text = implode(' ', $words); - } + $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } +/** + * Trims text to a certain number of words. + * + * @since 3.3.0 + * + * @param string $text Text to trim. + * @param int $num_words Number of words. Default 55. + * @param string $more What to append if $text needs to be trimmed. Default '…'. + * @return string Trimmed text. + */ +function wp_trim_words( $text, $num_words = 55, $more = null ) { + if ( null === $more ) + $more = __( '…' ); + $original_text = $text; + $text = strip_tags( $text ); + $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); + if ( count( $words_array ) > $num_words ) { + array_pop( $words_array ); + $text = implode( ' ', $words_array ); + $text = $text . $more; + } else { + $text = implode( ' ', $words_array ); + } + return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); +} + /** * Converts named entities into numbered entities. *