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 > $sublenreturn 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);  

可单独截取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. if(strlen($title)<=$lenreturn substr($title,$start,$len); 
  20. $alen = 0;  
  21. $blen = 0; 
  22. $realnum = 0; 
  23. for($i=$start;$i<strlen($title);$i++)  
  24. {  
  25. $ctype = 0;  
  26. $cstep = 0;  
  27. $cur = substr($title,$i,1);  
  28. if($cur == "&")  
  29. {  
  30. if(substr($title,$i,4) == "<")  
  31. {  
  32. $cstep = 4;  
  33. $length += 4;  
  34. $i += 3;  
  35. $realnum ++;  
  36. if($magic)  
  37. {  
  38. $alen ++;  
  39. }  
  40. }  
  41. else if(substr($title,$i,4) == ">")  
  42. {  
  43. $cstep = 4;  
  44. $length += 4;  
  45. $i += 3;  
  46. $realnum ++;  
  47. if($magic)  
  48. {  
  49. $alen ++;  
  50. }  
  51. }  
  52. else if(substr($title,$i,5) == "&")  
  53. {  
  54. $cstep = 5;  
  55. $length += 5;  
  56. $i += 4;  
  57. $realnum ++;  
  58. if($magic)  
  59. {  
  60. $alen ++;  
  61. }  
  62. }  
  63. else if(substr($title,$i,6) == """)  
  64. {  
  65. $cstep = 6;  
  66. $length += 6;  
  67. $i += 5;  
  68. $realnum ++;  
  69. if($magic)  
  70. {  
  71. $alen ++;  
  72. }  
  73. }  
  74. else if(substr($title,$i,6) == "'")  
  75. {  
  76. $cstep = 6;  
  77. $length += 6;  
  78. $i += 5;  
  79. $realnum ++;  
  80. if($magic)  
  81. {  
  82. $alen ++;  
  83. }  
  84. }  
  85. else if(preg_match("/&#(d+);/i",substr($title,$i,8),$match))  
  86. {  
  87. $cstep = strlen($match[0]);  
  88. $length += strlen($match[0]);  
  89. $i += strlen($match[0])-1;  
  90. $realnum ++;  
  91. if($magic)  
  92. {  
  93. $blen ++;  
  94. $ctype = 1;  
  95. }  
  96. }  
  97. }else{  
  98. if(ord($cur)>=128)  
  99. {  
  100. $cstep = 2;  
  101. $length += 2;  
  102. $i += 1;  
  103. $realnum ++;  
  104. if($magic)  
  105. {  
  106. $blen ++;  
  107. $ctype = 1;  
  108. }  
  109. }else{  
  110. $cstep = 1;  
  111. $length +=1;  
  112. $realnum ++;  
  113. if($magic)  
  114. {  
  115. $alen++;  
  116. }  
  117. }  
  118. if($magic)  
  119. {  
  120. if(($blen*2+$alen) == ($len*2)) break;  
  121. if(($blen*2+$alen) == ($len*2+1))  
  122. {  
  123. if($ctype == 1)  
  124. {  
  125. $length -= $cstep;  
  126. break;  
  127. }else{  
  128. break;  
  129. }  
  130. }  
  131. }else{  
  132. if($realnum == $lenbreak;  
  133. }  
  134. unset($cur);  
  135. unset($alen);  
  136. unset($blen);  
  137. unset($realnum);  
  138. unset($ctype);  
  139. unset($cstep); 
  140. return substr($title,$start,$length);  
分享到:
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技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……