1

I was helped not long ago with a form of custom truncating by Toscho on this post it works very well but I'm finding that I need to create multiple length variations for different bits of content. For instance on my support page I need to 80 max_chars vs. my home page which I need 200 max_chars.

I tried adding a second instance of this function but unsurprisingly it totally killed other aspects of my site.

As always, any help would be appreciated. Thanks guys!

function utf8_truncate( $string, $max_chars = 200, $append = "\xC2\xA0…" )
{
    $string = strip_tags( $string );
    $string = html_entity_decode( $string, ENT_QUOTES, 'utf-8' );
    // \xC2\xA0 is the no-break space
    $string = trim( $string, "\n\r\t .-;–,—\xC2\xA0" );
    $length = strlen( utf8_decode( $string ) );

    // Nothing to do.
    if ( $length < $max_chars )
    {
        return $string;
    }

    // mb_substr() is in /wp-includes/compat.php as a fallback if
    // your the current PHP installation doesn't have it.
    $string = mb_substr( $string, 0, $max_chars, 'utf-8' );

    // No white space. One long word or chinese/korean/japanese text.
    if ( FALSE === strpos( $string, ' ' ) )
    {
        return $string . $append;
    }

    // Avoid breaks within words. Find the last white space.
    if ( extension_loaded( 'mbstring' ) )
    {
        $pos   = mb_strrpos( $string, ' ', 'utf-8' );
        $short = mb_substr( $string, 0, $pos, 'utf-8' );
    }
    else
    {
        // Workaround. May be slow on long strings.
        $words = explode( ' ', $string );
        // Drop the last word.
        array_pop( $words );
        $short = implode( ' ', $words );
    }

    return $short . $append;
}

1 Answer 1

1

I know toscho doesn't like this very much, but anyway: Converted the input args to an array:

function utf8_truncate( $args = array( 'string' => null, 'max_chars' => 200, 'append' => "\xC2\xA0…" ) )
{
    $args['string'] = strip_tags( $args['string'] );
    $args['string'] = html_entity_decode( $args['string'], ENT_QUOTES, 'utf-8' );
    // \xC2\xA0 is the no-break space
    $args['string'] = trim( $args['string'], "\n\r\t .-;–,—\xC2\xA0" );
    $length = strlen( utf8_decode( $args['string'] ) );

    // Nothing to do.
    if ( $length < $args['max_chars'] )
    {
        return $args['string'];
    }

    // mb_substr() is in /wp-includes/compat.php as a fallback if
    // your the current PHP installation doesn't have it.
    $args['string'] = mb_substr( $args['string'], 0, $args['max_chars'], 'utf-8' );

    // No white space. One long word or chinese/korean/japanese text.
    if ( FALSE === strpos( $args['string'], ' ' ) )
    {
        return $args['string'] . $args['append'];
    }

    // Avoid breaks within words. Find the last white space.
    if ( extension_loaded( 'mbstring' ) )
    {
        $pos   = mb_strrpos( $args['string'], ' ', 'utf-8' );
        $short = mb_substr( $args['string'], 0, $pos, 'utf-8' );
    }
    else
    {
        // Workaround. May be slow on long strings.
        $words = explode( ' ', $args['string'] );
        // Drop the last word.
        array_pop( $words );
        $short = implode( ' ', $words );
    }

    return $short . $args['append'];
}

This allows to use it like this (you possibily missed how to use arrays anyway):

$args = array(
     'string' => 'bla'
    ,'max_chars' => 50 // INPUT LENGTH HERE
);
echo '<p>' . utf8_truncate( $args ) . '</p>';

You could also switch this on demand:

if ( is_page() )
{
    $args['max_chars'] = 100;
}
elseif ( is_archive() )
{
    $args['max_chars'] = 50;
}
elseif ( is_whatever() )
    ... etc ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.