首页 > 代码库 > 截取中文字符串
截取中文字符串
public static function cn_truncate($string, $strlen = 20, $etc = ‘...‘, $keep_first_style = false, $charset = ‘utf-8‘)
{
$slen = mb_strlen($string, $charset);
if ($slen > $strlen+2)
{
$tstr = mb_substr($string, 0, $strlen, $charset);
$matches = array();
$mcount = preg_match_all("/[\x{4e00}-\x{9fa5}]/u", $tstr, $matches);
unset($matches);
$offset = ($strlen - $mcount) * 0.35;//0;//intval((3*mb_strlen($tstr,$charset)-strlen($tstr))*0.35);
return preg_replace(‘/\&\w*$/‘, ‘‘, mb_substr($string, 0, $strlen + $offset, $charset)) . $etc;
}
else
{
return $string;
}
}
截取中文字符串