复制代码 代码如下:
/* UTF-8中文字符截断程序 */
$str = "123这是测试字符串";
$str1 = "()()";
echo subUTF8str($str,0,3)."
";
echo subUTF8str($str,0,4)."
";
echo subUTF8str($str1,0,4)."
";
echo subUTF8str($str1,0,10)."
";
function subUTF8str($str,$start=0,$length=80){
$cur_len = 0; //人理解的字符串长度
$all_len = strlen($str); //机器理解字符串长度
if($length > $all_len)
{
return $str;
}
for($i = 0;$i {
if($cur_len == $start)
{
break;
}
if (ord($str[$i]) > 127)
{
$i += 3;
}else{
$i += 1;
}
$cur_len ++;
}
$start_pos = $i;
$temp_pos = $cur_len;
for(;$cur_len - $temp_pos {
if($i >= $all_len)
break;
if (ord($str[$i]) > 127)
{
$i += 3;
}else{
$i += 1;
}
$cur_len ++;
}
$end_pos = $i;
return substr($str,$start_pos,$end_pos);
}
?>
其实,PHP原生就有多charset下字符截取方案,额,所以就是这个样子...囧..
Multibyte String Functions函数族中,
string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] ) 用来字符串截取
int mb_strlen ( string $str [, string $encoding ] ) 返回字符串长度
....
详细请查看PHP手册
http://www.bkjia.com/PHPjc/326024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326024.htmlTechArticle复制代码 代码如下: ?php /* UTF-8中文字符截断程序 */ $str = "123这是测试字符串"; $str1 = "()()"; echo subUTF8str($str,0,3)."br"; echo subUTF8str($str,0,4)...