Abstract word-trimming from wp_trim_excerpt() into wp_trim_words(). Props nacin. Fixes #16372.

git-svn-id: http://svn.automattic.com/wordpress/trunk@18732 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
duck_ 2011-09-20 17:14:23 +00:00
parent 5daf7aa2b0
commit 8cfc0fa5ec
1 changed files with 27 additions and 9 deletions

View File

@ -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.
*