php专区

 首页 > php专区 > PHP应用 > php函数大全 > php怎么截取中文字符串 - php函数

php怎么截取中文字符串 - php函数

分享到:
【字体:大 中 小】
导读:
          在php中截取字符串最简单的办法就是利用substr()函数来实现,但是substr函数只能截取英文,如果是中文就会是乱码,那么有朋友说可使用mb_subst...

php怎么截取中文字符串

在php中截取字符串最简单的办法就是利用substr()函数来实现,但是substr函数只能截取英文,如果是中文就会是乱码,那么有朋友说可使用mb_substr()来截取,这个方法又不能截取中文英混合的字符。

此函数用于截取gb2312编码的中文字符串,代码如下:

  1. // 说明:截取中文字符串 
  2. function mysubstr($str, $start, $len) {  
  3.     $tmpstr = "";  
  4.     $strlen = $start + $len;  
  5.     for($i = 0; $i < $strlen; $i++) {  
  6.         if(ord(substr($str, $i, 1)) > 0xa0) {  
  7.             $tmpstr .= substr($str, $i, 2);  
  8.             $i++;  
  9.         } else 
  10.             $tmpstr .= substr($str, $i, 1);  
  11.     }  
  12.     return $tmpstr;  
  13. }  
  14. ?> 

Utf-8、gb2312都支持的汉字截取函数,截取utf-8字符串函数.

为了支持多语言,数据库里的字符串可能保存为UTF-8编码,在网站开发中可能需要用php截取字符串的一部分,为了避免出现乱码现象,编写如下的UTF-8字符串截取函数

UTF-8编码的字符可能由1~3个字节组成,具体数目可以由第一个字节判断出来,理论上可能更长,但这里假设不超过3个字节

第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符,第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符,否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号).

代码如下:

  1. // 说明:Utf-8、gb2312都支持的汉字截取函数  
  2.    
  3. /*  
  4. Utf-8、gb2312都支持的汉字截取函数  
  5. cut_str(字符串, 截取长度, 开始长度, 编码);  
  6. 编码默认为 utf-8  
  7. 开始长度默认为 0  
  8. */ 
  9.    
  10. function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')  
  11. {  
  12.     if($code == 'UTF-8')  
  13.     {  
  14.         $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]/";  
  15.         preg_match_all($pa, $string, $t_string);  
  16.    
  17.         if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";  
  18.         return join('', array_slice($t_string[0], $start, $sublen));  
  19.     }  
  20.     else 
  21.     {  
  22.         $start = $start*2;  
  23.         $sublen = $sublen*2;  
  24.         $strlen = strlen($string);  
  25.         $tmpstr = '';  
  26.    
  27.         for($i=0; $i<$strlen; $i++)  
  28.         {  
  29.             if($i>=$start && $i<($start+$sublen))  
  30.             {  
  31.                 if(ord(substr($string, $i, 1))>129)  
  32.                 {  
  33.                     $tmpstr.= substr($string, $i, 2);  
  34.                 }  
  35.                 else 
  36.                 {  
  37.                     $tmpstr.= substr($string, $i, 1);  
  38.                 }  
  39.             }  
  40.             if(ord(substr($string, $i, 1))>129) $i++;  
  41.         }  
  42.         if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";  
  43.         return $tmpstr;  
  44.     }  
  45. }  
  46.    
  47. $str = "abcd需要截取的字符串";  
  48. echo cut_str($str, 8, 0, 'gb2312');  
  49. ?> 

代码如下:

  1. function utf8Substr($str, $from, $len)  
  2. {  
  3.     return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.  
  4.                        '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',  
  5.                        '$1',$str);  
  6. } 

可单独截取uft8字符串。

程序说明:

1. len 参数以中文字符为标准,1len等于2个英文字符,为了形式上好看些

2. 如果将magic参数设为false,则中文和英文同等看待,取绝对的字符数

3. 特别适用于用htmlspecialchars()进行过编码的字符串

4. 能正确处理GB2312中实体字符模式

程序代码:

  1. function FSubstr($title,$start,$len="",$magic=true)  
  2. {  
  3. /**  
  4. * powered by Smartpig  
  5. * mailto:d.einstein@263.net  
  6. */ 
  7. $length = 0;  
  8. if($len == "") $len = strlen($title); 
  9. //判断起始为不正确位置  
  10. if($start > 0)  
  11. {  
  12. $cnum = 0;  
  13. for($i=0;$i<$start;$i++)  
  14. {  
  15. if(ord(substr($title,$i,1)) >= 128) $cnum ++;  
  16. }  
  17. if($cnum%2 != 0) $start--; 
  18. unset($cnum);  
  19. } 
  20. if(strlen($title)<=$len) return substr($title,$start,$len); 
  21. $alen = 0;  
  22. $blen = 0; 
  23. $realnum = 0; 
  24. for($i=$start;$i<strlen($title);$i++)  
  25. {  
  26. $ctype = 0;  
  27. $cstep = 0;  
  28. $cur = substr($title,$i,1);  
  29. if($cur == "&")  
  30. {  
  31. if(substr($title,$i,4) == "<")  
  32. {  
  33. $cstep = 4;  
  34. $length += 4;  
  35. $i += 3;  
  36. $realnum ++;  
  37. if($magic)  
  38. {  
  39. $alen ++;  
  40. }  
  41. }  
  42. else if(substr($title,$i,4) == ">")  
  43. {  
  44. $cstep = 4;  
  45. $length += 4;  
  46. $i += 3;  
  47. $realnum ++;  
  48. if($magic)  
  49. {  
  50. $alen ++;  
  51. }  
  52. }  
  53. else if(substr($title,$i,5) == "&")  
  54. {  
  55. $cstep = 5;  
  56. $length += 5;  
  57. $i += 4;  
  58. $realnum ++;  
  59. if($magic)  
  60. {  
  61. $alen ++;  
  62. }  
  63. }  
  64. else if(substr($title,$i,6) == """)  
  65. {  
  66. $cstep = 6;  
  67. $length += 6;  
  68. $i += 5;  
  69. $realnum ++;  
  70. if($magic)  
  71. {  
  72. $alen ++;  
  73. }  
  74. }  
  75. else if(substr($title,$i,6) == "'")  
  76. {  
  77. $cstep = 6;  
  78. $length += 6;  
  79. $i += 5;  
  80. $realnum ++;  
  81. if($magic)  
  82. {  
  83. $alen ++;  
  84. }  
  85. }  
  86. else if(preg_match("/&#(d+);/i",substr($title,$i,8),$match))  
  87. {  
  88. $cstep = strlen($match[0]);  
  89. $length += strlen($match[0]);  
  90. $i += strlen($match[0])-1;  
  91. $realnum ++;  
  92. if($magic)  
  93. {  
  94. $blen ++;  
  95. $ctype = 1;  
  96. }  
  97. }  
  98. }else{  
  99. if(ord($cur)>=128)  
  100. {  
  101. $cstep = 2;  
  102. $length += 2;  
  103. $i += 1;  
  104. $realnum ++;  
  105. if($magic)  
  106. {  
  107. $blen ++;  
  108. $ctype = 1;  
  109. }  
  110. }else{  
  111. $cstep = 1;  
  112. $length +=1;  
  113. $realnum ++;  
  114. if($magic)  
  115. {  
  116. $alen++;  
  117. }  
  118. }  
  119. } 
  120. if($magic)  
  121. {  
  122. if(($blen*2+$alen) == ($len*2)) break;  
  123. if(($blen*2+$alen) == ($len*2+1))  
  124. {  
  125. if($ctype == 1)  
  126. {  
  127. $length -= $cstep;  
  128. break;  
  129. }else{  
  130. break;  
  131. }  
  132. }  
  133. }else{  
  134. if($realnum == $len) break;  
  135. }  
  136. } 
  137. unset($cur);  
  138. unset($alen);  
  139. unset($blen);  
  140. unset($realnum);  
  141. unset($ctype);  
  142. unset($cstep); 
  143. return substr($title,$start,$length);  
  144. } 
分享到:
php file_get_contents与curl()函数对比 ...
php file_get_contents与curl()函数对比 在php中file_get_contents与curl()函数都可以用来抓取对方网站的数据并保存到本地服务器中,但是总得来讲file_get_contents()效率稍低些,常用失败的情况、curl()效率挺高的,支持多线程,不过需要开启下curl扩展,也就是说要使用curl函数就必须要打开curl...
php ob_start(ob_gzhandler)进行网页压缩...
php ob_start(ob_gzhandler)进行网页压缩 先来看ob_start用法,使用PHP ob_start()函数打开browser的cache,这样可以保证cache的内容在你调用flush(),ob_end_flush()(或程序执行完毕)之前不会被输出,代码如下:
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……