Home  >  Article  >  Backend Development  >  PHP intercepts mixed Chinese and English strings

PHP intercepts mixed Chinese and English strings

WBOY
WBOYOriginal
2016-07-25 08:50:55900browse
  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));
复制代码


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