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); ?>