php怎么截取中文字符串

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

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

  1. <?php  
  2. // 说明:截取中文字符串 
  3. function mysubstr($str$start$len) {  
  4.     $tmpstr = "";  
  5.     $strlen = $start + $len;  
  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. ?> 

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

代码如下:

  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);  
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php怎么截取中文字符串

38 评论

  1. prednisone 20mg price buy 5mg cialis viagra 100mg generic

  2. best essay writing websites write my paper blackjack vegas free online games

  3. buy levitra sale lyrica sale medrol 8 mg over the counter

  4. buy naprosyn 250mg online cheap oral omnicef prevacid sale

  5. cheap ramipril buy altace 10mg order arcoxia 60mg without prescription

  6. olmesartan 10mg drug cost verapamil buy depakote for sale

  7. order nexium 40mg online cheap nexium ca order lasix for sale

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡