When using php built-in functions to intercept Chinese characters, you may encounter question marks. Below are some examples of Chinese characters accurately intercept. Php has only two problems with operating strings.
When using php built-in functions to intercept Chinese characters, you may encounter question marks. Below are some examples of Chinese characters accurately intercept.
The time for php to operate on strings is nothing more than two problems:
1. determine whether the string encoding is gbk or unicode.
2. extract the corresponding encoding.
In general, we may encounter garbled characters when using substr to intercept Chinese characters, because Chinese characters are dubyte characters. when a byte is intercepted, this Chinese character cannot be displayed and is out of order.
In fact, the solution is very simple. See the following screenshot function. the code is as follows:
- // Truncate an extra long string
- Function curtStr ($ str, $ len = 30 ){
- If (strlen ($ str)> $ len ){
- $ Str = substr ($ str, 0, $ len );
- $ Str. = chr (0 )."... ";
- Return $ str;
- }
The above chr (0) is not null
Null is nothing, while chr (0) is 0. The hexadecimal value is 0 × 00, and the binary value is 00000000.
Although chr (0) does not display 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 to 0xff and 0x00 is always displayed as "null". Based on this feature, add a chr (0) after the result of substr ), this prevents garbled characters.
The following are some functions that can be used to precisely intercept Chinese strings. the utf8 encoded multi-byte string is truncated. the code is as follows:
-
- // Truncate the utf8 string
- Function utf8Substr ($ str, $ from, $ len)
- {
- Return preg_replace ('# ^ (? : [X00-x7F] | [xC0-xFF] [x80-xBF] +) {0, '. $ from .'}'.
- '((? : [X00-x7F] | [xC0-xFF] [x80-xBF] +) {0, '. $ len.'}). * # s ',
- '$ 1', $ str );
- }
- ?>
UTF-8, GB2312 support Chinese character truncation function, the code is as follows:
-
- /*
- Chinese character truncation functions supported by Utf-8 and gb2312
- Cut_str (string, truncation length, start length, encoding );
- The default encoding format is UTF-8.
- The default start length is 0.
- */
-
- Function cut_str ($ string, $ sublen, $ start = 0, $ code = 'utf-8 ')
- {
- If ($ code = 'utf-8 ')
- {
- $ 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]/";
- Preg_match_all ($ pa, $ string, $ t_string );
-
- If (count ($ t_string [0])-$ start> $ sublen) return join ('', array_slice ($ t_string [0], $ start, $ sublen )). "... ";
- Return join ('', array_slice ($ t_string [0], $ start, $ sublen ));
- }
- Else
- {
- $ Start = $ start * 2;
- $ Sublen = $ sublen * 2;
- $ Strlen = strlen ($ string );
- $ Tmpstr = '';
-
- For ($ I = 0; $ I <$ strlen; $ I ++)
- {
- If ($ I >=$ start & $ I <($ start + $ sublen ))
- {
- If (ord (substr ($ string, $ I, 1)> 129)
- {
- $ Tmpstr. = substr ($ string, $ I, 2 );
- }
- Else
- {
- $ Tmpstr. = substr ($ string, $ I, 1 );
- }
- }
- If (ord (substr ($ string, $ I, 1)> 129) $ I ++;
- }
- If (strlen ($ tmpstr) <$ strlen) $ tmpstr. = "...";
- Return $ tmpstr;
- }
- }
-
- $ Str = "the string to be intercepted by abcd ";
- Echo cut_str ($ str, 8, 0, 'gb2312 ');
- ?>