PHP Chinese string truncation without garbled Solution

Source: Internet
Author: User

A good string truncation function:

 Function Substring ( $ Str , $ Start , $ Length ){ //  Relatively easy to use string truncation Functions      $ Len = $ Length  ;  If ( $ Length <0 ){  $ Str = Strrev ($ Str  );  $ Len =- $ Length  ;}  $ Len = ( $ Len < Strlen ( $ Str ))? $ Len : Strlen ( $ Str  );  $ Tmpstr = "" ; For ( $ I = $ Start ; $ I < $ Len ; $ I ++ ){  If ( ORD ( Substr ( $ Str , $ I , 1)> 0xa0 ){ $ Tmpstr . = Substr ( $ Str , $ I , 2 );  $ I ++ ;}  Else  {  $ Tmpstr . = Substr ( $ Str , $ I , 1 );}} If ( $ Length <0) $ Tmpstr = Strrev ( $ Tmpstr  );  Return   $ Tmpstr  ;} 

Example:

 $ Str1 = 'I am a long string of Chinese without English' ;  $ Str2 = 'I am a long string of Chinese characters with yingwen' ;  $ Len =Strlen ( $ Str1  );  Echo '<Br/> '. $ Len ; //  Return 28  $ Len = Strlen ( $ Str2  );  Echo '<Br/> '. $ Len ; //  Return 29 Echo '<Br/>' ;  Echo Substring ( $ Str1 , 0, 11 );  Echo '<Br/>' ;  Echo Substring ( $ Str2 , 0, 11 );  Echo '<Br/>' ;  Echo Substring ( $ Str1 , 16, 28 );  Echo '<Br/>' ;  Echo Substring ( $ Str2 , 16, 29 );

Result:

 
2829I'm a string of comparisons. I'm a string of comparisons.

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:

 Function Formatname ( $ Str , $ Size  ){  $ Len = Strlen ($ Str  );  If ( Strlen ( $ Str )> $ Size  ){  $ Part1 = Substring ( $ Str , 0, $ Size /2 );  $ Part2 = Substring ( $ Str , $ Len -($ Size /2 ), $ Len  );  Return   $ Part1 ."...". $ Part2  ;}  Else  {  Return   $ Str  ;}} 

In addition, I saw a super simple Chinese truncation solution on the Internet. I tried it and it worked well:

 
Echo Substr($ Str1, 0, 10 ).CHR(0 );

Principles:

CHR (0) is not null
07null is nothing, while CHR (0) is 0. The hexadecimal value is 0x00, And the binary value is 00000000.
08 although CHR (0) does not show anything, It is a character.
09 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"
10 based on this feature, add a CHR (0) after the substr result 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:
 Function Strlen_utf8 ( $ Str  ){  $ Len = Strlen ($ Str  );  $ N = 0 ;  For ( $ I = 0; $ I < $ Len ; $ I ++ ){  $ X = Substr ( $ Str , $ I , 1);  $ = Base_convert ( ORD ( $ X ), 10, 2 );  $ = Substr ('20140901 '. $ ,-8 );  If ( Substr ( $ , 0, 1) = 0 ){} Elseif ( Substr ( $ , 0, 3) = 110 ){  $ I + = 1 ;}  Elseif ( Substr ( $ , 0, 4) = 1110 ){  $ I + = 2 ;}  $ N ++ ;} Return   $ N  ;}  //  End strlen_utf8; 

String truncation function:

 Function Substring_utf8 ( $ Str , $ Start , $ Lenth  ){  $ Len = Strlen ( $ Str  ); $ R = Array  ();  $ N = 0 ;  $ M = 0 ;  For ( $ I = 0; $ I < $ Len ; $ I ++ ){  $ X =Substr ( $ Str , $ I , 1 );  $ = Base_convert ( ORD ( $ X ), 10, 2 );  $ = Substr ('20140901 '. $ ,-8 );  If ( $ N < $ Start  ){  If ( Substr ( $ , 0, 1) = 0 ){}  Elseif ( Substr ( $ , 0, 3) = 110 ){  $ I + = 1 ;} Elseif ( Substr ( $ , 0, 4) = 1110 ){  $ I + = 2 ;}  $ N ++ ;}  Else  {  If ( Substr ( $ , 0, 1) = 0){  $ R [] = Substr ( $ Str , $ I , 1 );}  Elseif ( Substr ( $ , 0, 3) = 110 ){  $ R [] = Substr ( $ Str ,$ I , 2 );  $ I + = 1 ;}  Elseif ( Substr ( $ , 0, 4) = 1110 ){  $ R [] = Substr ( $ Str , $ I , 3 ); $ I + = 2 ;}  Else  {  $ R [] ='' ;}  If (++ $ M > = $ Lenth  ){  Break  ;}}}  Return   Join ($ R  );}  //  End substring_utf8; 

The usage method is the same as described earlier. For example, formatname can be implemented as follows (This optimizes the length of Chinese characters ):

 Function Formatname ( $ Str , $ Size  ){  $ Len = Strlen_utf8 ( $ Str  );  $ One_len = Strlen ( $ Str );  $ Size = $ Size * 1.5 * $ Len /( $ One_len  );  If (Strlen_utf8 ( $ Str )> $ Size  ){  $ Part1 = Substring_utf8 ( $ Str , 0, $ Size /2 ); $ Part2 = Substring_utf8 ( $ Str , $ Len -( $ Size /2 ), $ Len  );  Return   $ Part1 ."...". $ Part2  ;}  Else  {  Return   $ Str  ;}} 

 

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.