Php Chinese character string truncation garbled problem solution

Source: Internet
Author: User
The problem of Chinese truncation garbled characters is generally Chinese text mixing more, if you intercept English will not be a problem, Chinese will have, the main reason is: String Encoding for UTF-8, a Chinese character

The problem of Chinese truncation garbled characters is generally Chinese text mixing more, if you intercept English will not be a problem, Chinese will have, the main reason is: String Encoding for UTF-8, A Chinese character occupies three bytes while a string is encoded as GB2312. a Chinese character occupies two bytes. let's take a look at the example.

The string is encoded as GB2312, and a Chinese character occupies two bytes. the code is as follows:

  1. Public static function chinesesubstr ($ str, $ start, $ len) {// $ str indicates the string, $ start indicates the start position of the string, and $ len indicates the string length.
  2. $ Strlen = $ start + $ len; // use $ strlen to store the total length of the string, that is, from the start position of the string to the total length of the string
  3. For ($ I = $ start; $ I <$ strlen ;){
  4. If (ord (substr ($ str, $ I, 1)> 0xa0) {// if the ASCII order value of the first byte in the string is greater than 0xa0, it indicates Chinese characters
  5. $ Tmpstr. = substr ($ str, $ I, 2); // Extract two characters each time and assign them to the variable $ tmpstr, which is equal to one Chinese character.
  6. $ I = $ I + 2; // variable auto-Increment 2
  7. } Else {
  8. $ Tmpstr. = substr ($ str, $ I, 1); // if it is not a Chinese character, a character is taken out each time and assigned to the variable $ tmpstr
  9. $ I ++;
  10. }
  11. }
  12. Return $ tmpstr; // return a string
  13. }

Character string encoded as UTF-8, a Chinese character occupies three bytes, the code is as follows:

  1. Public static function chinesesubstr ($ str, $ start, $ len) {// $ str indicates the string, $ start indicates the start position of the string, and $ len indicates the string length.
  2. $ Strlen = $ start + $ len; // use $ strlen to store the total length of the string, that is, from the start position of the string to the total length of the string
  3. For ($ I = $ start; $ I <$ strlen ;){
  4. If (ord (substr ($ str, $ I, 1)> 0xa0) {// if the ASCII order value of the first byte in the string is greater than 0xa0, it indicates Chinese characters
  5. $ Tmpstr. = substr ($ str, $ I, 3); // each time, three characters are taken out and assigned to the variable $ tmpstr, which is equal to a Chinese character.
  6. $ I = $ I + 3; // variable auto-Increment 3
  7. } Else {
  8. $ Tmpstr. = substr ($ str, $ I, 1); // if it is not a Chinese character, a character is taken out each time and assigned to the variable $ tmpstr
  9. $ I ++;
  10. }
  11. }
  12. Return $ tmpstr; // return a string
  13. }

Although this problem has been solved above, it is relatively troublesome to pay attention to the encoding problem. The following describes a solution that no matter what encoding is required. the code is as follows:

  1. /**
  2. * Chinese character truncation functions supported by Utf-8 and gb2312
  3. * Cut_str (string, truncation length, start length, encoding );
  4. * The default encoding format is UTF-8.
  5. * The default start length is 0.
  6. */
  7. Function cut_str ($ string, $ sublen, $ start = 0, $ code = 'utf-8 ')
  8. {
  9. If ($ code = 'utf-8 ')
  10. {
  11. $ Pa = "/[x01-x7f] | [xc2-xdf] [x80-xbf] | xe0 [xa0-xbf] [x80-xbf] | [xe1-xef] [x80-xbf] [x80-xbf] | xf0 [x90-xbf] [x80-xbf]] [x80-xbf] | [xf1-xf7] [x80-xbf] [x80-xbf] [x80-xbf]/";
  12. Preg_match_all ($ pa, $ string, $ t_string );
  13. If (count ($ t_string [0])-$ start> $ sublen) return join ('', array_slice ($ t_string [0], $ start, $ sublen )). "... ";
  14. Return join ('', array_slice ($ t_string [0], $ start, $ sublen ));
  15. }
  16. Else
  17. {
  18. $ Start = $ start * 2;
  19. $ Sublen = $ sublen * 2;
  20. $ Strlen = strlen ($ string );
  21. $ Tmpstr = '';
  22. For ($ I = 0; $ I <$ strlen; $ I ++)
  23. {
  24. If ($ I >=$ start & $ I <($ start + $ sublen ))
  25. {
  26. If (ord (substr ($ string, $ I, 1)> 129)
  27. {
  28. $ Tmpstr. = substr ($ string, $ I, 2 );
  29. }
  30. Else
  31. {
  32. $ Tmpstr. = substr ($ string, $ I, 1 );
  33. }
  34. }
  35. If (ord (substr ($ string, $ I, 1)> 129) $ I ++;
  36. }
  37. If (strlen ($ tmpstr) <$ strlen) $ tmpstr. = "... ";
  38. Return $ tmpstr;
  39. }
  40. }

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.