Home >Backend Development >PHP Tutorial >Solve the problem of garbled characters when intercepting UTF-8 Chinese strings in PHP

Solve the problem of garbled characters when intercepting UTF-8 Chinese strings in PHP

WBOY
WBOYOriginal
2016-07-25 09:04:19978browse
  1. /**
  2. @php intercepts utf-8 Chinese string garbled characters
  3. @link http://bbs.it-home.org
  4. */
  5. function utf8_substr($str,$len)
  6. {
  7.   for($i=0;$i<$len;$i++)
  8.   {
  9.     $temp_str=substr($str,0,1);
  10.     if(ord($temp_str) > 127){
  11.       $i++;
  12.     if($i<$len){
  13.       $new_str[]=substr($str,0,3);
  14.       $str=substr($str,3);
  15.       }
  16.     }else {
  17.     $new_str[]=substr($str,0,1);
  18.     $str=substr($str,1);
  19.     }
  20.   }
  21.   return join($new_str);
  22. }
  23. ?>
复制代码


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