PHP 判断字符串长度及字符串切割问题

1.字符串长度

PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改

[php]  view plain copy
  1. /** 
  2. * PHP获取字符串中英文混合长度  
  3. * @param $str string 字符串 
  4. * @param $charset string 编码 
  5. * @return 返回长度,1中文=1位,2英文=1位 
  6. */  
  7. function strLength($str,$charset='utf-8'){  
  8. if($charset=='utf-8'$str = iconv('utf-8','gb2312',$str);  
  9. $num = strlen($str);  
  10. $cnNum = 0;  
  11. for($i=0;$i<$num;$i++){  
  12. if(ord(substr($str,$i+1,1))>127){  
  13. $cnNum++;  
  14. $i++;  
  15. }  
  16. }  
  17. $enNum = $num-($cnNum*2);  
  18. $number = ($enNum/2)+$cnNum;  
  19. return ceil($number);  
  20. }  
  21.   
  22. //测试输出长度都为15  
  23. $str1 = '测试测试测试测试测试测试测试测';  
  24. $str2 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';  
  25. $str3 = 'aa测试aa测试aa测试aa测试aaaaaa';  
  26. echo strLength($str1,'gb2312');  
  27. echo strLength($str2,'gb2312');  
  28. echo strLength($str3,'gb2312');  

2.截取字符串函数

UTF8编码,在UTF8中,一个中文字符占3个字节

[php]  view plain copy
  1. function msubstr($str$start$len) {  
  2.     $tmpstr = "";  
  3.     $strlen = $start + $len;  
  4.     for($i = 0; $i < $strlen$i++){  
  5.         if(ord(substr($str$i, 1)) > 127){  
  6.             $tmpstr.=substr($str$i, 3);  
  7.             $i+=2;  
  8.         }else  
  9.             $tmpstr.= substr($str$i, 1);  
  10.     }  
  11.     return $tmpstr;  
  12. }  
  13. echo msubstr("一二三天下致公english",0,10);  

GB2312编码,在gb2312中,一个中文字符占2个字节

[php]  view plain copy
  1. <?php  
  2. function msubstr($str$start$len) {   //ȡ  
  3.    $tmpstr = "";  
  4.    $strlen = $start + $len;  
  5.    if(preg_match('/[/d/s]{2,}/',$str)){$strlen=$strlen-2;}  
  6.    for($i = 0; $i < $strlen$i++) {  
  7.        if(ord(substr($str$i, 1)) > 0xa0) {  
  8.            $tmpstr .= substr($str$i, 2);  
  9.            $i++;  
  10.        } else  
  11.            $tmpstr .= substr($str$i, 1);  
  12.      }  
  13.    return $tmpstr;  
  14.  }  
  15.     
  16. ?>  

编码兼容性良好的函数

[php]  view plain copy
  1. function cc_msubstr($str$start=0, $length$charset="utf-8"$suffix=true)  
  2. {  
  3.     if(function_exists("mb_substr"))  
  4.         return mb_substr($str$start$length$charset);  
  5.     elseif(function_exists('iconv_substr')) {  
  6.         return iconv_substr($str,$start,$length,$charset);  
  7.     }  
  8.     $re['utf-8']   = "/[/x01-/x7f]|[/xc2-/xdf][/x80-/xbf]|[/xe0-/xef][/x80-/xbf]{2}|[/xf0-/xff][/x80-/xbf]{3}/";  
  9.     $re['gb2312'] = "/[/x01-/x7f]|[/xb0-/xf7][/xa0-/xfe]/";  
  10.     $re['gbk']    = "/[/x01-/x7f]|[/x81-/xfe][/x40-/xfe]/";  
  11.     $re['big5']   = "/[/x01-/x7f]|[/x81-/xfe]([/x40-/x7e]|/xa1-/xfe])/";  
  12.     preg_match_all($re[$charset], $str$match);  
  13.     $slice = join("",array_slice($match[0], $start$length));  
  14.     if($suffixreturn $slice."…";  
  15.     return $slice;  
  16. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值