5 января 2011 г.

Cut UTF-8 string without breaking words

A PHP function to cut UTF-8 string by spaces without breaking words:
if (!function_exists('cut_utf8_string')) {
function cut_utf8_string($str, $len) {
if (mb_strlen($str, 'UTF-8') > $len){
$str = mb_substr($str, 0, $len, 'UTF-8');
$pos = mb_strrpos($str, ' ', false, 'UTF-8');
if($pos === false) {
return mb_substr($str, 0, $len, 'UTF-8').'…';
}
return mb_substr($str, 0, $pos, 'UTF-8').'…';
}else{
return $str;
}
}
}


(modified function by blog.insicdesigns.com)

Комментариев нет: