PHP截取中文字符串方法详解

php自带的一个截取字符串函数substr,但其只能处理英文、数字却不能截取中文混排的。如果需要在PHP中进行GB2312与UTF-8的互换,需要php_iconv.dll的支持(PHP4中包含此文件)。php5内建支持iconv,更加方便了。不管是uft-8编码转换为gb2312,还是将 gb2312 转换为 uft-8,PHP4.3.1以后的iconv函数很方便,只是需要自己写一个uft8到unicode的转换函数。处理中文的字符串截取函数mb_substr()是在PHP4.0.6后引入的,其自身支持不同编码字符的处理,所以一些新的PHP框架其实已经支持mb_substr()了。


处理函数汇总:

[php]  view plain  copy
  1. function cutstr($string$length$dot = ' ...')   
  2. {   //截字符串函数    GBK,UTF8  
  3.     $charset = 'utf-8';  
  4.       
  5.     if(strlen($string) <= $length)  
  6.     {   //边界条件  
  7.         return $string;  
  8.     }  
  9.   
  10.     $string = str_replace(array('&''"''<''>'), array('&''"''<''>'), $string);  
  11.       
  12.     $strcut = '';  
  13.     if(strtolower($charset) == 'utf-8') {  
  14.   
  15.         $n = $tn = $noc = 0;  
  16.         while($n < strlen($string)) {  
  17.   
  18.         $t = ord($string[$n]);  
  19.         if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {  
  20.             $tn = 1; $n++; $noc++;  
  21.         } elseif(194 <= $t && $t <= 223) {  
  22.             $tn = 2; $n += 2; $noc += 2;  
  23.         } elseif(224 <= $t && $t <= 239) {  
  24.             $tn = 3; $n += 3; $noc += 2;  
  25.         } elseif(240 <= $t && $t <= 247) {  
  26.             $tn = 4; $n += 4; $noc += 2;  
  27.         } elseif(248 <= $t && $t <= 251) {  
  28.             $tn = 5; $n += 5; $noc += 2;  
  29.         } elseif($t == 252 || $t == 253) {  
  30.             $tn = 6; $n += 6; $noc += 2;  
  31.         } else {  
  32.             $n++;  
  33.         }  
  34.   
  35.         if($noc >= $length) {  
  36.             break;  
  37.         }  
  38.   
  39.     }  
  40.     if($noc > $length)   
  41.     {  
  42.         $n -= $tn;  
  43.     }  
  44.   
  45.     $strcut = substr($string, 0, $n);  
  46.   
  47.     } else  
  48.     {  
  49.         for($i = 0; $i < $length$i++)  
  50.         {  
  51.             $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];  
  52.         }  
  53.     }  
  54.   
  55.     $strcut = str_replace(array('&''"''<''>'), array('&''"''<''>'), $strcut);  
  56.   
  57.     return $strcut.$dot;  
  58. }  

方法二:


[php]  view plain  copy
  1. function len($string$sublen = 80, $etc = '...',$break_words = false, $middle = false)  
  2. {  
  3. $start=0;  
  4. $code="UTF-8";  
  5.        if($code == 'UTF-8')   
  6.    {   
  7.        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";   
  8.        preg_match_all($pa$string$t_string);  
  9.        if(count($t_string[0]) - $start > $sublenreturn join(''array_slice($t_string[0], $start$sublen))."...";   
  10.        return join(''array_slice($t_string[0], $start$sublen));   
  11.    }   
  12.    else   
  13.    {   
  14.        $start = $start*2;   
  15.        $sublen = $sublen*2;   
  16.        $strlen = strlen($string);   
  17.        $tmpstr = '';  
  18.        for($i=0; $i<$strlen$i++)   
  19.        {   
  20.            if($i>=$start && $i<($start+$sublen))   
  21.            {   
  22.                if(ord(substr($string$i, 1))>129)   
  23.                {   
  24.                    $tmpstr.= substr($string$i, 2);   
  25.                }   
  26.                else   
  27.                {   
  28.                    $tmpstr.= substr($string$i, 1);   
  29.                }   
  30.            }   
  31.            if(ord(substr($string$i, 1))>129) $i++;   
  32.        }   
  33.        if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";   
  34.        return $tmpstr;   
  35.    }  
  36. }  

方法三(兼容mb_substr):

[php]  view plain  copy
  1. /** 
  2.  +---------------------------------------------------------- 
  3.  * 字符串截取,支持中文和其他编码 
  4.  +---------------------------------------------------------- 
  5.  * @static 
  6.  * @access public 
  7.  +---------------------------------------------------------- 
  8.  * @param string $str 需要转换的字符串 
  9.  * @param string $start 开始位置 
  10.  * @param string $length 截取长度 
  11.  * @param string $charset 编码格式 
  12.  * @param string $suffix 截断显示字符 
  13.  +---------------------------------------------------------- 
  14.  * @return string 
  15.  +---------------------------------------------------------- 
  16.  */  
  17. function msubstr($str$start$length$charset="utf-8"$suffix=true)  
  18. {  
  19.     if(function_exists("mb_substr")){  
  20.         $slice = mb_substr($str$start$length$charset);  
  21.     }elseif(function_exists('iconv_substr')) {  
  22.         $slice = iconv_substr($str,$start,$length,$charset);  
  23.         if(false === $slice) {  
  24.             $slice = '';  
  25.         }  
  26.     }else{  
  27.         $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";  
  28.         $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";  
  29.         $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";  
  30.         $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";  
  31.         preg_match_all($re[$charset], $str$match);  
  32.         $slice = join("",array_slice($match[0], $start$length));  
  33.     }  
  34.     return $suffix ? $slice.'...' : $slice;  
  35. }  



你可能感兴趣的:(PHP)