PHP calculates Chinese string lengths, and functions to intercept Chinese strings

Source: Internet
Author: User
  1. Header (' Content-type:text/html;charset=utf-8 ');
  2. /**
  3. * Statistics Chinese string length function
  4. * @param $str string to calculate the length
  5. * @param $type calculated length type, 0 (default) means one character in Chinese, 1 for a Chinese figure of two characters
  6. * @http://bbs.it-home.org
  7. *
  8. */
  9. function Abslength ($STR)
  10. {
  11. if (empty ($STR)) {
  12. return 0;
  13. }
  14. if (function_exists (' Mb_strlen ')) {
  15. Return Mb_strlen ($str, ' utf-8 ');
  16. }
  17. else {
  18. Preg_match_all ("/./u", $str, $ar);
  19. return count ($ar [0]);
  20. }
  21. }
  22. $STR = ' Scripting Academy welcome everyone, Ye!. ';
  23. $len = Abslength ($STR);
  24. Var_dump ($len); Return 12
  25. $len = Abslength ($str, ' 1 ');
  26. Echo '
    '. $len; return 22
  27. /*
  28. Utf-8 code to intercept Chinese strings, parameters can refer to the SUBSTR function
  29. @param $str the string to intercept
  30. @param $start start position to intercept, negative number is reverse intercept
  31. @param $end the length to be intercepted
  32. */
  33. function Utf8_substr ($str, $start =0) {
  34. if (empty ($STR)) {
  35. return false;
  36. }
  37. if (function_exists (' mb_substr ')) {
  38. if (Func_num_args () >= 3) {
  39. $end = Func_get_arg (2);
  40. Return Mb_substr ($str, $start, $end, ' utf-8 ');
  41. }
  42. else {
  43. Mb_internal_encoding ("UTF-8");
  44. Return Mb_substr ($str, $start);
  45. }
  46. }
  47. else {
  48. $null = "";
  49. Preg_match_all ("/./u", $str, $ar);
  50. if (Func_num_args () >= 3) {
  51. $end = Func_get_arg (2);
  52. Return join ($null, array_slice ($ar [0], $start, $end));
  53. }
  54. else {
  55. Return join ($null, array_slice ($ar [0], $start));
  56. }
  57. }
  58. }
  59. $str 2 = ' wo to intercept Zhongwen ';
  60. Echo '
    ';
  61. Echo utf8_substr ($str 2,0,-4); Return wo to intercept Zhon
  62. ?>
Copy Code

2, support Gb2312,gbk,utf-8,big5 Chinese interception method

  1. /*
  2. * Chinese interception, support Gb2312,gbk,utf-8,big5
  3. * bbs.it-home.org
  4. * @param string $str the string to intercept
  5. * @param int $start intercept start position
  6. * @param int $length intercept length
  7. * @param string $charset UTF-8|GB2312|GBK|BIG5 encoding
  8. * @param $suffix whether to add suffix
  9. */
  10. Public Function Csubstr ($STR, $start =0, $length, $charset = "Utf-8", $suffix =true)
  11. {
  12. if (function_exists ("Mb_substr"))
  13. {
  14. if (Mb_strlen ($str, $charset) <= $length) return $str;
  15. $slice = Mb_substr ($str, $start, $length, $charset);
  16. }
  17. Else
  18. {
  19. $re [' utf-8 '] = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff] [\X80-\XBF] {3}/";
  20. $re [' gb2312 '] = "/[\x01-\x7f]| [\xb0-\xf7] [\xa0-\xfe]/];
  21. $re [' gbk '] = "/[\x01-\x7f]| [\x81-\xfe] [\x40-\xfe]/];
  22. $re [' big5 '] = "/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe]) /";
  23. Preg_match_all ($re [$charset], $STR, $match);
  24. if (count ($match [0]) <= $length) return $str;
  25. $slice = Join ("", Array_slice ($match [0], $start, $length));
  26. }
  27. if ($suffix) return $slice. " ...";
  28. return $slice;
  29. }
  30. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.