PHP intercepts mixed Chinese and English strings

WBOY
Release: 2016-07-25 08:50:55
Original
864 people have browsed it
  1. $str='天Find我';
  2. function cut_str($str='',$offset,$length){
  3. $str=iconv("utf-8","gb2312",$str);
  4. $str_len=strlen($str);
  5. $arr=array();
  6. for($i=0;$i<$str_len;$i++){
  7. if(ord($str{$i})>0x80){
  8. $arr[]=$str{$i}.$str{$i+1};
  9. $i++;
  10. continue;
  11. }
  12. $arr[]=$str{$i};
  13. }
  14. return array_slice($arr,$offset,$length);
  15. }
  16. var_dump(cut_str($str,0,2));
复制代码


Related labels:
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!