public
Last active

function utf8_truncate() for WordPress

  • Download Gist
utf8_truncate.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php
/**
* Shortens an UTF-8 encoded string without breaking words.
*
* @link http://wordpress.stackexchange.com/q/11085/11089#11089
* @param string $string string to shorten
* @param int $max_chars maximal length in characters
* @param string $append replacement for truncated words.
* @return string
*/
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;
}

Hi, thank's for this extension:)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.