Kyle Goslan
Kyle Goslan

Reputation: 10920

Modify wp_trim_words function to return content split in 2

Im trying to modify the wp_trim_words function to return the left over words as well as the first part, any help much appreciated.

function wp_trim_words_new( $text, $num_words = 55, $more = null ) {
if ( null === $more )
    $more = __( '…' );
$original_text = $text;
$text = wp_strip_all_tags( $text );
/* translators: If your word count is based on single characters (East Asian characters),
   enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
    $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
    preg_match_all( '/./u', $text, $words_array );
    $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
    $sep = '';
} else {
    $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
    $sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
    array_pop( $words_array );
    $text = implode( $sep, $words_array );
    $text = $text . $more;
} else {
    $text = implode( $sep, $words_array );
}
/**
 * Filter the text content after words have been trimmed.
 *
 * @since 3.3.0
 *
 * @param string $text          The trimmed text.
 * @param int    $num_words     The number of words to trim the text to. Default 5.
 * @param string $more          An optional string to append to the end of the trimmed text, e.g. ….
 * @param string $original_text The text before it was trimmed.
 */
return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );

}

Upvotes: 2

Views: 1312

Answers (3)

You can accomplish this without the wp_trim_words function.

$sentence = "This is a sample sentence";
    $words = explode(" ", $sentence);
    $half = count($words) / 2;
    $first_half = implode(" ", array_slice($words, 0, $half));
    $second_half = implode(" ", array_slice($words, $half));
    
    echo $first_half . "\n";
    echo $second_half;

Upvotes: 0

Kim Steinhaug
Kim Steinhaug

Reputation: 502

Not sure you need to tweak that function at all, this code does what you ask:

$trimmed_text = wp_trim_words($text, $wordlength, '');

// Measure full and trimmed widths for comparison
$fw = mb_strwidth($text);
$tw = mb_strwidth($trimmed_text);

if( $fw != $tw ){
    $clipped_text = mb_strimwidth($text, $tw, $fw - $tw, '');
    // Text has been cut
} else {
    // Text has NOT been cut
}

or wrap it in a function behaving as you wanted initially

function wp_trim_words_new($text, $length, $delimiter=''){
    $trimmed_text = wp_trim_words($text, $length, $delimiter);
    $fw = mb_strwidth($text);
    $tw = mb_strwidth($trimmed_text);
    if( $fw != $tw ){
        return [$trimmed_text, mb_strimwidth($text, $tw, $fw - $tw, $delimiter)];
    } else {
        return [$text, null];
    }
}

Upvotes: 0

Walter Tross
Walter Tross

Reputation: 12624

It seems to me that you simply copied the trim_words() function of Wordpress. You could have done better. Your desired output is unclear.

Supposing that you would be happy with the simple tag-less string of left-over words returned together with the output of the original function, you could use something like this (untested):

function wp_trim_words_2( $text, $num_words = 55, $more = null ) {
    if ( null === $more )
        $more = __( '…' );
    $original_text = $text;
    $text = wp_strip_all_tags( $text );
    /* translators: If your word count is based on single characters (East Asian characters),
       enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
    if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
        $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
        preg_match_all( '/\X/u', $text, $match_array );
        $split_array = $match_array[0];
        $sep = '';
    } else {
        $split_array = preg_split( "/[\n\r\t ]+/", $text, -1, PREG_SPLIT_NO_EMPTY );
        $sep = ' ';
    }
    $words_array = array_slice( $split_array, 0, $num_words + 1 );
    if ( count( $words_array ) > $num_words ) {
        array_pop( $words_array );
        $text = implode( $sep, $words_array );
        $text = $text . $more;
        $rest = implode( $sep, array_slice( $split_array, $num_words ) );
    } else {
        $text = implode( $sep, $words_array );
        $rest = '';
    }
    /**
     * Filter the text content after words have been trimmed.
     *
     * @since 3.3.0
     *
     * @param string $text          The trimmed text.
     * @param int    $num_words     The number of words to trim the text to. Default 5.
     * @param string $more          An optional string to append to the end of the trimmed text, e.g. ….
     * @param string $original_text The text before it was trimmed.
     */
    return array( apply_filters( 'wp_trim_words_2', $text, $num_words, $more, $original_text ), $rest );
}

to be called like this:

list($trimmed, $rest) = wp_trim_words_2($text);

I'm not sure about apply_filters(), though. Should it be passed the $rest too? It's too time-consuming for me to study this one, sorry.

If, on the other hand, you wanted the $rest before tag removal (i.e., before the call to wp_strip_all_tags()), then things are much more complicated, because the original functions splits the input into words after that, and I can think only of inefficient ways to keep the correspondence between the input and the output of wp_strip_all_tags().

BTW, the original code by WordPress contains a bug: words for East Asian languages are wrongly counted (and potentially split!) as single Unicode characters instead of Unicode graphemes. The fix is to use '/\X/u' instead of '/./u' (read here for an explanation).

Upvotes: 1

Related Questions