PHP中文字符截取
2008-03-31 15:48
399 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function chnSubstr($str,$start,$len){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$strlen=$start+$len;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$tmpstr = '';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$i = 0;//取值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$j = 0;//计数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while($j< $strlen && $j < strlen($str)){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$tmp1 = mb_substr($str,$i,1,"UTF-8"); //可以换成自己需要的字符编码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$mblen = strlen($tmp1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if($j == ($strlen-1) && $mblen > 2 && $len < strlen($tmpstr.$tmp1)){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return $tmpstr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$tmpstr .= $tmp1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if($mblen > 1){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$j++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
$j++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return $tmpstr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//用法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
echo chnSubstr($msg,15);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//要用此函数需要PHP支持mb_sring。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
?>
相关文章推荐
- PHP 字符截取 解决中文的截取问题,不用mb系列
- PHP截取中文字符出现乱码的具体解决办法
- php 中文字符截取函数
- PHP字符截取函数。兼容中文,英文,中英文混杂等情况
- php截取字符串(中文字符截取mb_substr)
- PHP 截取中文字符函数
- php 截取指定长度中文字符
- PHP substr截取中文字符出现…
- php实现中文字符截取防乱码方法汇总
- PHP截取中文字符无乱码 mb_substr()
- PHP截取字符串,获取长度,获取字符位置的函数 中文字符截取
- php完美截取中文字符函数mb_substr
- PHP中文字符截取方案(UTF8,GB编码截取)
- 自己在用的PHP中文字符截取函数
- php smarty截取中文字符乱码问题?gb2312/utf-8
- PHP substr截取中文字符出现乱码的问题解疑
- php中文字符截取防乱码
- php smarty截取中文字符乱码问题?gb2312/utf-8
- PHP 字符截取 解决中文的截取问题,不用mb系列
- php 解决substr()截取中文字符乱码问题