在每种语言中,都会遇到汉字的截取,原因英文字母占一个字节,汉字占2个字节,一般的函数中都是对单个字节的,就是说按照英文截取的,当遇到汉字时可能截取一半,就会出现乱码。下面是php的解决方案,其他语言类似。
Java代码
function msubstr($str, $start, $len){
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++){
if(ord(substr($str, $i, 1)) > 0xa0){
$tmpstr .= substr($str, $i, 2);
$i++;
}else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
function msubstr($str, $start, $len){
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++){
if(ord(substr($str, $i, 1)) > 0xa0){
$tmpstr .= substr($str, $i, 2);
$i++;
}else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
在smarty模板中,经常用到汉字的截取,需要修改truncate函数或是自己增加函数,下面的代码是增加的函数:
Java代码
function smarty_modifier_cntruncate($string, $strlen = 20, $etc = '...', $keep_first_style = false)
$strlen = $strlen*2;
$string = trim($string);
if ( strlen($string) <= $strlen ) {
return $string;
}
$str = strip_tags($string);
$j = 0;
for($i=0;$i<$strlen;$i++) {
if(ord(substr($str,$i,1))>0xa0) $j++;
}
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen) {$rstr .= $etc;}
if ( $keep_first_style == true && ereg('^<(.*)>$',$string) ) {
if ( strlen($str) <= $strlen ) {
return $string;
}
$start_pos = strpos($string,substr($str,0,4));
$end_pos = strpos($string,substr($str,-4));
$end_pos = $end_pos+4;
$rstr = substr($string,0,$start_pos) . $rstr . substr($string,$end_pos,strlen($string));
}
return $rstr;
function smarty_modifier_cntruncate($string, $strlen = 20, $etc = '...', $keep_first_style = false)
{
$strlen = $strlen*2;
$string = trim($string);
if ( strlen($string) <= $strlen ) {
return $string;
}
$str = strip_tags($string);
$j = 0;
for($i=0;$i<$strlen;$i++) {
if(ord(substr($str,$i,1))>0xa0) $j++;
}
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen) {$rstr .= $etc;}
if ( $keep_first_style == true && ereg('^<(.*)>$',$string) ) {
if ( strlen($str) <= $strlen ) {
return $string;
}
$start_pos = strpos($string,substr($str,0,4));
$end_pos = strpos($string,substr($str,-4));
$end_pos = $end_pos+4;
$rstr = substr($string,0,$start_pos) . $rstr . substr($string,$end_pos,strlen($string));
}
return $rstr;
}
用法说明:<{$kjswsList[kjswsCount].topic|cntruncate:18:"...":true}>
18为汉字的个数,不是字节数。
了解更多请访问:http://www.ndfweb.cn/news-566.html