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

WBOY
Release: 2016-07-25 09:04:19
Original
940 people have browsed it
  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. ?>
复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!