PHP UTF-8 string truncation function to achieve non-garbled truncation of Chinese Characters

Source: Internet
Author: User

The substr () function built in PHP cannot be used to truncate Chinese characters, and garbled characters in both Chinese and English may occur. The following two solutions are provided.

1. gb2312 encoding truncation,

 1   Function Msubstr ( $ Str , $ Start , $ Len  ){  2   3      If ( Strlen ( $ Str )- $ Start < $ Len ) Return   False  ;  4   5       $ Tmpstr = "" ;  6   7       $ Strlen = $ Start + $ Len  ;  8   9        For ($ I = 0; $ I < $ Strlen ; $ I ++ ){  10   11            If ( ORD ( Substr ( $ Str , $ I , 1)> 0xa0 ){ //  0xa0 indicates that the ASCII value of the first Chinese character encoding is greater than 0xa0. 12   13               $ Tmpstr . = Substr ( $ Str , $ I , 2 );  14   15               $ I ++ ;  16   17 } Else  18   19              $ Tmpstr . = Substr ( $ Str , $ I , 1 );  20   21   }  22   23        Return   $ Tmpstr ."..." ;  24   25 }

 

2. Chinese Character Truncation in utf8 format

The characters encoded by the UTF-8 may be 1 ~ It consists of three bytes. The specific number can be determined by the first byte. (Theoretically it may be longer, but it is assumed that the length cannot exceed 3 bytes)

The first byte is greater than 224, which together with the second byte after it forms a UTF-8 character

The first byte is greater than 192 less than 224, and it is a UTF-8 character with the first byte after it

Otherwise, the first byte is an English character (including numbers and a small part of punctuation marks ).

 1   //  $ Sourcestr is the string to be processed  2   3   // $ Cutlength is the truncation length (that is, the number of words)  4   5   Function Cut_str ( $ Sourcestr , $ Cutlength )  6   7   {  8   9      $ Returnstr ='' ;  10   11      $ I = 0 ;  12   13      $ N = 0 ;  14  15      $ Str_length = Strlen ( $ Sourcestr ); //  Number of bytes of a string  16   17      While (( $ N < $ Cutlength ) And ( $ I <= $ Str_length  ))  18  19   {  20   21         $ Temp_str = Substr ( $ Sourcestr , $ I , 1 );  22   23         $ Ascnum = ORD ( $ Temp_str ); // Returns the ASCII code of the $ I character in the string.  24   25         If ( $ Ascnum >=224) //  If the ASCII bit height is 224,  26   27   {  28   29            $ Returnstr = $ Returnstr . Substr ($ Sourcestr , $ I , 3 ); //  According to the UTF-8 encoding specification, count 3 consecutive characters as a single character  30   31            $ I = $ I + 3; //  The actual byte count is 3.  32   33            $ N ++; //  String Length meter 1 34   35   }  36   37          Elseif ( $ Ascnum >=192) //  If the ASCII bit height is 192,  38   39   {  40   41            $ Returnstr = $ Returnstr .Substr ( $ Sourcestr , $ I , 2 ); //  2 consecutive characters are counted as a single character according to the UTF-8 encoding Specification  42   43            $ I = $ I + 2; //  The actual byte count is 2.  44   45            $ N ++; // String Length meter 1  46   47   }  48   49          Elseif ( $ Ascnum > = 65 && $ Ascnum <= 90) //  If it is a capital letter,  50   51   {  52   53           $ Returnstr = $ Returnstr . Substr ( $ Sourcestr , $ I , 1 );  54   55            $ I = $ I + 1; //  The actual number of bytes is still counted as 1  56   57            $ N ++;//  However, considering the overall appearance, uppercase letters are counted as a high character.  58   59   }  60   61          Else                  //  In other cases, including lower-case letters and halfwidth punctuation marks,  62   63   {  64   65            $ Returnstr = $ Returnstr .Substr ( $ Sourcestr , $ I , 1 );  66   67            $ I = $ I + 1; //  The actual number of bytes is 1.  68   69            $ N = $ N + 0.5; // Lower-case letters, halfwidth punctuation, and half-height characters...  70   71   }  72   73   }  74   75             If ( $ Str_length > $ Cutlength  ){  76   77             $ Returnstr =$ Returnstr ."..."; //  When the length is exceeded, add a ellipsis at the end.  78   79   }  80   81        Return   $ Returnstr  ;  82   83 }

 

Reference: http://bupt-roy.iteye.com/blog/1179016

Http://zww.me/archives/25356

Http://imluren.com/2011/04/php-utf8-substr.html/comment-page-1

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.