Function to intercept Chinese characters-csubstr

WBOY
Release: 2016-07-25 09:07:16
Original
921 people have browsed it
  1. //--------------------------------- ------------------

  2. //Function name: csubstr
  3. //Function: truncate string, one Chinese character counts as two characters, and English character counts as one character
  4. //For GBK (GB2312) encoded website
  5. // Parameters: $str ---- original string
  6. // $start ---- starting length
  7. // $len ---- intercepted length
  8. // Return value: intercepted The last string
  9. //Organized by: itlearner
  10. //---------------------------------------- ----------------

  11. function csubstr($str, $start, $len)

  12. {
  13. if ($len >= strlen( $str)) return $str;
  14. $tmpstr = "";
  15. $len= ($len < strlen($str)) ? $len : strlen($str);
  16. for ($i= $start; $ i < $len; $i ++)
  17. {
  18. if (ord(substr($str, $i, 1)) > 0xa0)
  19. {
  20. $tmpstr .= substr($str, $i, 2) ;
  21. $i ++;
  22. } else
  23. {
  24. $tmpstr .= substr($str, $i, 1);
  25. }
  26. }
  27. $tmpstr .= "...";
  28. return $tmpstr;
  29. }
  30. ?>

Copy code


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!