Home >Backend Development >PHP Tutorial >PHP Chinese string interception PHP function to intercept Chinese string

PHP Chinese string interception PHP function to intercept Chinese string

WBOY
WBOYOriginal
2016-07-25 09:04:151008browse
  1. /**
  2. @Chinese string interception function
  3. @http://bbs.it-home.org
  4. */
  5. function cnSubStr($string,$sublen)
  6. {
  7. if($sublen>=strlen($string))
  8. {
  9. return $string;
  10. }
  11. $s="";
  12. for($i=0;$i<$sublen;$i++)
  13. {
  14. if(ord($string{$i})>127)
  15. {
  16. $s.=$string{$i}.$string{++$i};
  17. continue;
  18. }else{
  19. $s.=$string{$i};
  20. continue;
  21. }
  22. }
  23. return $s;
  24. }// End Function cnSubStr($string,$sublen)
  25. /////////// Use like this :
  26. echo "

    __________________________

    ";

  27. $string="382脚本学堂8923专业网站技术站点89023";
  28. $sublen=strlen($string);
  29. $len=20;
  30. echo $string."

    ";

  31. echo "总长为:".($sublen+1)."

    ";

  32. echo "截取数:".$len."

    ";

  33. for($i=1;$i<=$sublen+1;$i++){
  34. if($i>$len){
  35. echo $i." ".cnSubStr($string,$i)."…
    ";
  36. continue;
  37. }
  38. echo $i." ".cnSubStr($string,$i)."
    ";
  39. }
  40. ?>
复制代码


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