当用 substr 截取中文字符的时候会出现乱码,当然你如果装了 mb 扩展,用 mb_substr截取就不会出现这种问题,如果你用的是虚拟主机,主机上又没装 mb扩展的话,你可以用下面的函数截取
# Cut chinese string into appointed length
# @param [string] string need deal
# @param [start] position to cut from
# @param [length] length to cut
# @return string
function GBsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$len=$start+$length;
for($i=$start;$i<$len;$i++){
if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str.'...';
}else{
return $string;
}
}
# @param [string] string need deal
# @param [start] position to cut from
# @param [length] length to cut
# @return string
function GBsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$len=$start+$length;
for($i=$start;$i<$len;$i++){
if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str.'...';
}else{
return $string;
}
}