- Function substring ($ str, $ start, $ length) {// 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'
'. $ Len; // return 28
-
$ Len = strlen ($ str2 );
- Echo'
'. $ Len; // return 29
-
Echo'
';
- Echo substring ($ str1, 0, 11 );
- Echo'
';
- Echo substring ($ str2, 0, 11 );
- Echo'
';
- Echo substring ($ str1, 16, 28 );
- Echo'
';
- Echo substring ($ str2, 16, 29 );
- ?>
-
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:
- 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. after testing, the results were very good:
- Echo substr ($ str1, 0, 10). chr (0 );
- ?>
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:
-
Function strlen_UTF8 ($ str)
- {
- $ Len = strlen ($ str );
- $ N = 0;
- For ($ I = 0; $ I <$ len; $ I ++ ){
- $ X = substr ($ str, $ I, 1 );
- $ A = base_convert (ord ($ x), 10, 2 );
- $ A = substr ('20140901'. $ a,-8 );
- If (substr ($ a, 0, 1) = 0 ){
- } Elseif (substr ($ a, 0, 3) = 110 ){
- $ I + = 1;
- } Elseif (substr ($ a, 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 );
- $ A = base_convert (ord ($ x), 10, 2 );
- $ A = substr ('20140901'. $ a,-8 );
- If ($ n <$ start ){
- If (substr ($ a, 0, 1) = 0 ){
- } Elseif (substr ($ a, 0, 3) = 110 ){
- $ I + = 1;
- } Elseif (substr ($ a, 0, 4) = 1110 ){
- $ I + = 2;
- }
- $ N ++;
- } Else {
- If (substr ($ a, 0, 1) = 0 ){
- $ R [] = substr ($ str, $ I, 1 );
- } Elseif (substr ($ a, 0, 3) = 110 ){
- $ R [] = substr ($ str, $ I, 2 );
- $ I + = 1;
- } Elseif (substr ($ a, 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;
- }
- }
- ?>
|