PHP截取字符串 兼容utf-8 gb2312

<?php
function subString($string,$length,$append = false) {
if(strlen($string) <= $length ) {
return $string;
}else{
$i = 0;
while ($i < $length){
$stringTMP = substr($string,$i,1);
if ( ord($stringTMP) >=224 ){
$stringTMP = substr($string,$i,3);
$i = $i + 3;
}elseif( ord($stringTMP) >=192 ){
$stringTMP = substr($string,$i,2);
$i = $i + 2;
}else{
$i = $i + 1;
}
$stringLast[] = $stringTMP;
}
$stringLast = implode("",$stringLast);
if($append){
$stringLast .= "...";
}
return $stringLast;
}
} $string = "输不起de青春 —— phper";
$length = "26";
$append = true;
echo subString($string,$length,$append);
?>
上一篇:前端程序员应该知道的 15 个 jQuery 小技巧


下一篇:如何发布带静态资源的库——android 篇