I got this function:
function shorter($text, $chars_limit) {
if (strlen($text) > $chars_limit)
return substr($text, 0, strrpos(substr($text, 0, $ch
If you are looking for a function that trims some actual text, you will probably need a UTF-8 safe function. Also, if you want to trim the text somewhat intelligently (trim the text after alphanumeric characters only, no HTML and so on) you can try this funciton I wrote:
/**
* shortens the supplied text after last word
* @param string $string
* @param int $max_length
* @param string $end_substitute text to append, for example "..."
* @param boolean $html_linebreaks if LF entities should be converted to
* @return string
*/
function mb_word_wrap($string, $max_length, $end_substitute = null, $html_linebreaks = true) {
if($html_linebreaks) $string = preg_replace('/\
/i', "\n", $string);
$string = strip_tags($string); //gets rid of the HTML
if(empty($string) || mb_strlen($string) <= $max_length) {
if($html_linebreaks) $string = nl2br($string);
return $string;
}
if($end_substitute) $max_length -= mb_strlen($end_substitute, 'UTF-8');
$stack_count = 0;
while($max_length > 0){
$char = mb_substr($string, --$max_length, 1, 'UTF-8');
if(preg_match('#[^\p{L}\p{N}]#iu', $char)) $stack_count++; //only alnum characters
elseif($stack_count > 0) {
$max_length++;
break;
}
}
$string = mb_substr($string, 0, $max_length, 'UTF-8').$end_substitute;
if($html_linebreaks) $string = nl2br($string);
return $string;
}