Php Chinese character string truncation and no garbled solution

Source: Internet
Author: User
Php Chinese character string truncation and no garbled solution

  1. Function substring ($ str, $ start, $ length) {// use string truncation functions.
  2. $ Len = $ length;
  3. If ($ length <0 ){
  4. $ Str = strrev ($ str );
  5. $ Len =-$ length;
  6. }
  7. $ Len = ($ len <strlen ($ str ))? $ Len: strlen ($ str );
  8. $ Tmpstr = "";
  9. For ($ I = $ start; $ I <$ len; $ I ++)
  10. {
  11. If (ord (substr ($ str, $ I, 1)> 0xa0)
  12. {
  13. $ Tmpstr. = substr ($ str, $ I, 2 );
  14. $ I ++;
  15. } Else {
  16. $ Tmpstr. = substr ($ str, $ I, 1 );
  17. }
  18. }
  19. If ($ length <0) $ tmpstr = strrev ($ tmpstr );
  20. Return $ tmpstr;
  21. }
  22. ?>

Example:

  1. $ Str1 = 'I am a long string of Chinese without English ';

  2. $ Str2 = 'I am a long string of Chinese characters with yingwen ';

  3. $ Len = strlen ($ str1 );

  4. Echo'
    '. $ Len; // return 28

  5. $ Len = strlen ($ str2 );

  6. Echo'
    '. $ Len; // return 29

  7. Echo'
    ';

  8. Echo substring ($ str1, 0, 11 );
  9. Echo'
    ';
  10. Echo substring ($ str2, 0, 11 );
  11. Echo'
    ';
  12. Echo substring ($ str1, 16, 28 );
  13. Echo'
    ';
  14. Echo substring ($ str2, 16, 29 );
  15. ?>

The result shows: 2829 I am comparing a string. I am comparing a string of Chinese characters without English letters and with yingwen.

This function is very useful. for example, it is used to intercept long file names, but you need to add... in the middle. you can do this:

  1. Function formatName ($ str, $ size ){
  2. $ Len = strlen ($ str );
  3. If (strlen ($ str)> $ size ){
  4. $ Part1 = substring ($ str, 0, $ size/2 );
  5. $ Part2 = substring ($ str, $ len-($ size/2), $ len );
  6. Return $ part1. "..."... $ part2;
  7. } Else {
  8. Return $ str;
  9. }
  10. }
  11. ?>

In addition, I saw a super simple Chinese truncation solution on the internet. after testing, the results were very good:

  1. Echo substr ($ str1, 0, 10). chr (0 );
  2. ?>

Principle explanation: chr (0) is not nullnull, but chr (0) is 0. The hexadecimal value is 0x00, indicating that the binary value is 00000000. although chr (0) does not show anything, it is a character. When a Chinese character is truncated, according to the encoding rules, he always needs to pull the other characters behind it as an explanation of the Chinese character. this is the cause of garbled characters. The combination of 0x81, 0 x FF, and 0x00 is always displayed as "null". Based on this feature, add a chr (0) to the result of substr to prevent garbled characters.

20120705 update: Although the above method is good, but occasionally encounter garbled characters, the reason is not further explored. However, you can use the following methods to try UTF8 text. Note: In this method, the length of a Chinese character is calculated as 1 unit length, and the length of an English letter is 1 unit length. Calculation method of length:

  1. Function strlen_UTF8 ($ str)

  2. {
  3. $ Len = strlen ($ str );
  4. $ N = 0;
  5. For ($ I = 0; $ I <$ len; $ I ++ ){
  6. $ X = substr ($ str, $ I, 1 );
  7. $ A = base_convert (ord ($ x), 10, 2 );
  8. $ A = substr ('20140901'. $ a,-8 );
  9. If (substr ($ a, 0, 1) = 0 ){
  10. } Elseif (substr ($ a, 0, 3) = 110 ){
  11. $ I + = 1;
  12. } Elseif (substr ($ a, 0, 4) = 1110 ){
  13. $ I + = 2;
  14. }
  15. $ N ++;
  16. }
  17. Return $ n;
  18. } // End strlen_UTF8;

  19. /// String truncation function:

  20. Function subString_UTF8 ($ str, $ start, $ lenth)
  21. {
  22. $ Len = strlen ($ str );
  23. $ R = array ();
  24. $ N = 0;
  25. $ M = 0;
  26. For ($ I = 0; $ I <$ len; $ I ++ ){
  27. $ X = substr ($ str, $ I, 1 );
  28. $ A = base_convert (ord ($ x), 10, 2 );
  29. $ A = substr ('20140901'. $ a,-8 );
  30. If ($ n <$ start ){
  31. If (substr ($ a, 0, 1) = 0 ){
  32. } Elseif (substr ($ a, 0, 3) = 110 ){
  33. $ I + = 1;
  34. } Elseif (substr ($ a, 0, 4) = 1110 ){
  35. $ I + = 2;
  36. }
  37. $ N ++;
  38. } Else {
  39. If (substr ($ a, 0, 1) = 0 ){
  40. $ R [] = substr ($ str, $ I, 1 );
  41. } Elseif (substr ($ a, 0, 3) = 110 ){
  42. $ R [] = substr ($ str, $ I, 2 );
  43. $ I + = 1;
  44. } Elseif (substr ($ a, 0, 4) = 1110 ){
  45. $ R [] = substr ($ str, $ I, 3 );
  46. $ I + = 2;
  47. } Else {
  48. $ R [] = '';
  49. }
  50. If (++ $ m >=$ lenth ){
  51. Break;
  52. }
  53. }
  54. }
  55. Return join ($ r );
  56. } // End subString_UTF8;

  57. // The Usage method is the same as described earlier. for example, formatName can be implemented as follows (this optimizes the length of Chinese characters ):

  58. Function formatName ($ str, $ size ){
  59. $ Len = strlen_UTF8 ($ str );
  60. $ One_len = strlen ($ str );
  61. $ Size = $ size * 1.5 * $ len/($ one_len );
  62. If (strlen_UTF8 ($ str)> $ size ){
  63. $ Part1 = subString_UTF8 ($ str, 0, $ size/2 );
  64. $ Part2 = subString_UTF8 ($ str, $ len-($ size/2), $ len );
  65. Return $ part1. "..."... $ part2;
  66. } Else {
  67. Return $ str;
  68. }
  69. }
  70. ?>

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.