Home  >  Article  >  Backend Development  >  详解PHP截取中文字符串的具体代码实现_PHP教程

详解PHP截取中文字符串的具体代码实现_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:30:081033browse

任何一种语言都有专门针对中文处理的函数,往往这些处理方法都是比较难掌握的一部分。今天我们就向大家具体讲一下有关截取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. ?>   

以上代码示例就是PHP截取中文字符串的相关实现方式。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446308.htmlTechArticle任何一种语言都有专门针对中文处理的函数,往往这些处理方法都是比较难掌握的一部分。今天我们就向大家具体讲一下有关 截取GB2312中文...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn