WordPress.org

Make WordPress Core

Changeset 18732


Ignore:
Timestamp:
09/20/11 17:14:23 (4 years ago)
Author:
duck_
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r18705 r18732  
    18641864        $text = apply_filters('the_content', $text); 
    18651865        $text = str_replace(']]>', ']]>', $text); 
    1866         $text = strip_tags($text); 
    18671866        $excerpt_length = apply_filters('excerpt_length', 55); 
    18681867        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); 
    1869         $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); 
    1870         if ( count($words) > $excerpt_length ) { 
    1871             array_pop($words); 
    1872             $text = implode(' ', $words); 
    1873             $text = $text . $excerpt_more; 
    1874         } else { 
    1875             $text = implode(' ', $words); 
    1876         } 
     1868        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); 
    18771869    } 
    18781870    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 
     1871} 
     1872 
     1873/** 
     1874 * Trims text to a certain number of words. 
     1875 * 
     1876 * @since 3.3.0 
     1877 * 
     1878 * @param string $text Text to trim. 
     1879 * @param int $num_words Number of words. Default 55. 
     1880 * @param string $more What to append if $text needs to be trimmed. Default '…'. 
     1881 * @return string Trimmed text. 
     1882 */ 
     1883function wp_trim_words( $text, $num_words = 55, $more = null ) { 
     1884    if ( null === $more ) 
     1885        $more = __( '…' ); 
     1886    $original_text = $text; 
     1887    $text = strip_tags( $text ); 
     1888    $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); 
     1889    if ( count( $words_array ) > $num_words ) { 
     1890        array_pop( $words_array ); 
     1891        $text = implode( ' ', $words_array ); 
     1892        $text = $text . $more; 
     1893    } else { 
     1894        $text = implode( ' ', $words_array ); 
     1895    } 
     1896    return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); 
    18791897} 
    18801898 
Note: See TracChangeset for help on using the changeset viewer.