php截取中文函数
2012-04-18 16:55
302 查看
<?php //截取中文字符函数一 function cutstr($string, $length, $dot = ' ...' ,$charset = 'utf-8') { if(strlen($string) <= $length) { return $string; } $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string); $strcut = ''; if(strtolower($charset) == 'utf-8') { $n = $tn = $noc = 0; while($n < strlen($string)) { $t = ord($string[$n]); if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { $tn = 1; $n++; $noc++; } elseif(194 <= $t && $t <= 223) { $tn = 2; $n += 2; $noc += 2; } elseif(224 <= $t && $t < 239) { $tn = 3; $n += 3; $noc += 2; } elseif(240 <= $t && $t <= 247) { $tn = 4; $n += 4; $noc += 2; } elseif(248 <= $t && $t <= 251) { $tn = 5; $n += 5; $noc += 2; } elseif($t == 252 || $t == 253) { $tn = 6; $n += 6; $noc += 2; } else { $n++; } if($noc >= $length) { break; } } if($noc > $length) { $n -= $tn; } $strcut = substr($string, 0, $n); } else { for($i = 0; $i < $length; $i++) { $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; } } $strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut); return $strcut.$dot; } ?>
相关文章推荐
- 收藏几个支持中文的PHP字符串截取函数
- php用正则表达式截取中文字符串的函数
- PHP截取字符串,获取长度,获取字符位置的函数 中文字符截取
- 多个PHP中文字符串截取函数
- php截取中文字符串函数实例
- PHP基础12-自定义函数截取中文字符串
- php字符串截取函数(支持中文 utf-8截取)
- [转载 php]php截取utf8编码的中文函数
- php字符串截取函数,支持中文截取
- php中支持多种编码的中文字符串截取函数!
- PHP的两个常用函数截取中文字符串无乱码与获取客户端IP地址
- 没事写了两个函数,php中gbk和utf-8中文无乱码截取
- [转载 php]php截取utf8编码的中文函数
- php中支持多种编码的中文字符串截取函数!
- 收藏几个支持中文的PHP字符串截取函数
- PHP中文字符串截取无乱码方法,php汉字截取函数_preg_split()
- PHP截取含中文的混合字符串长度的函数
- PHP中一个中文字符串截取函数
- 多个PHP中文字符串截取函数
- php中计算中文字符串长度、截取中文字符串的函数代码