以下函数适用utf-8
function TRC($str,$len){
global $TRC_TYPE;
if(strlen($str)<=$len) return $str;
for($i=0;$i<$len;$i++){
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127){
$i++;
if($i<$len){
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}else{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
if($TRC_TYPE) return join($new_str).'...';
else return join($new_str);
}
以下函数适用gbk
function TRC($str,$len){
$r_str='';
$i=0;
while ($i<$len){
$ch=substr($str,$i,1);
if(ord($ch)>0x80) $i++;
$i++;
}
$r_str=substr($str,0,$i);
return $r_str;
}