Home >Backend Development >PHP Tutorial >PHP Chinese string interception function example

PHP Chinese string interception function example

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:12:52807browse

Example, Chinese string interception function code.

  1. //Example

  2. /*
  3. echo csubstr("123中文jbxue.com",0,4);
  4. echo csubstr("123中文jbxue国人" ,2,6);
  5. */

  6. //---Chinese string interception---

  7. function csubstr($str,$start,$len)
  8. {
  9. $strlen= strlen($str);
  10. if ($start>=$strlen)
  11. return $str;
  12. $clen=0;
  13. for($i=0;$i<$strlen;$i++,$clen++)
  14. {
  15. if(ord(substr($str,$i,1))>0xa0)
  16. {
  17. if ($clen>=$start)
  18. $tmpstr.=substr($str,$i,2);
  19. $i++ ;
  20. }
  21. else
  22. {
  23. if ($clen>=$start)
  24. $tmpstr.=substr($str,$i,1);
  25. }
  26. if ($clen>=$start+$len)
  27. break;
  28. }
  29. return $tmpstr;
  30. }

Copy code


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