How does php intercept Chinese strings. The simplest way to intercept a string in php is to use the substr () function, but the substr function can only intercept english. if it is a Chinese character, it will not be garbled, some friends say that the simplest way to intercept strings in php is to use the substr () function, but the substr function can only intercept English characters. if it is a Chinese character, it is not garbled, some friends say that they can use mb_substr () to intercept the English and Chinese characters.
This function is used to extract gb2312 encoded Chinese strings:
The code is as follows: |
|
// Description: truncates a Chinese string. Function mysubstr ($ str, $ start, $ len ){ $ Tmpstr = ""; $ Strlen = $ start + $ len; For ($ I = 0; $ I <$ strlen; $ I ++ ){ If (ord (substr ($ str, $ I, 1)> 0xa0 ){ $ Tmpstr. = substr ($ str, $ I, 2 ); $ I ++; } Else $ Tmpstr. = substr ($ str, $ I, 1 ); } Return $ tmpstr; } ?> |
Chinese character truncation functions supported by Utf-8 and gb2312
Truncates UTF-8 string functions.
To support multiple languages, strings in the database may be saved as UTF-8 encoding, and part of the string may need to be captured using php during website development. To avoid garbled characters, write the following UTF-8 string truncation function
For the principle of UTF-8, please refer to the UTF-8 FAQ
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 ).
The code is as follows: |
|
// Description: Utf-8, gb2312 support Chinese character truncation function /* 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 '); ?>
|
Note:
The code is as follows: |
|
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 ); } |
The uft8 string can be intercepted separately.
Program description:
1. the len parameter is based on Chinese characters. 1len is equal to 2 English characters.
2. if the magic parameter is set to false, the Chinese character and English character count are equivalent.
3. special character strings that have been encoded with htmlspecialchars ()
4. correct handling of the GB2312 entity character mode)
Program code:
The code is as follows: |
|
Function FSubstr ($ title, $ start, $ len = "", $ magic = true) { /** * Powered by Smartpig * Mailto: d.einstein@263.net */ $ Length = 0; If ($ len = "") $ len = strlen ($ title ); // Judge the starting position as incorrect If ($ start> 0) { $ Cnum = 0; For ($ I = 0; $ I <$ start; $ I ++) { If (ord (substr ($ title, $ I, 1) >=128) $ cnum ++; } If ($ cnum % 2! = 0) $ start --; Unset ($ cnum ); } If (strlen ($ title) <= $ len) return substr ($ title, $ start, $ len ); $ Alen = 0; $ Blen = 0; $ Realnum = 0; For ($ I = $ start; $ I { $ Ctype = 0; $ Cstep = 0; $ Cur = substr ($ title, $ I, 1 ); If ($ cur = "&") { If (substr ($ title, $ I, 4) = "<") { $ Cstep = 4; $ Length + = 4; $ I + = 3; $ Realnum ++; If ($ magic) { $ Alen ++; } } Else if (substr ($ title, $ I, 4) = "> ") { $ Cstep = 4; $ Length + = 4; $ I + = 3; $ Realnum ++; If ($ magic) { $ Alen ++; } } Else if (substr ($ title, $ I, 5) = "&") { $ Cstep = 5; $ Length + = 5; $ I + = 4; $ Realnum ++; If ($ magic) { $ Alen ++; } } Else if (substr ($ title, $ I, 6) = """) { $ Cstep = 6; $ Length + = 6; $ I + = 5; $ Realnum ++; If ($ magic) { $ Alen ++; } } Else if (substr ($ title, $ I, 6) = "'") { $ Cstep = 6; $ Length + = 6; $ I + = 5; $ Realnum ++; If ($ magic) { $ Alen ++; } } Else if (preg_match ("/& # (d +);/I", substr ($ title, $ I, 8), $ match )) { $ Cstep = strlen ($ match [0]); $ Length + = strlen ($ match [0]); $ I + = strlen ($ match [0])-1; $ Realnum ++; If ($ magic) { $ Blen ++; $ Ctype = 1; } } } Else { If (ord ($ cur) >=128) { $ Cstep = 2; $ Length + = 2; $ I + = 1; $ Realnum ++; If ($ magic) { $ Blen ++; $ Ctype = 1; } } Else { $ Cstep = 1; $ Length + = 1; $ Realnum ++; If ($ magic) { $ Alen ++; } } } If ($ magic) { If ($ blen * 2 + $ alen) = ($ len * 2) break; If ($ blen * 2 + $ alen) = ($ len * 2 + 1 )) { If ($ ctype = 1) { $ Length-= $ cstep; Break; } Else { Break; } } } Else { If ($ realnum = $ len) break; } } Unset ($ cur ); Unset ($ alen ); Unset ($ blen ); Unset ($ realnum ); Unset ($ ctype ); Unset ($ cstep ); Return substr ($ title, $ start, $ length ); } |
The substring () function is implemented, but the substr function can only intercept english. if it is a Chinese character, it is not garbled, so some friends say it can be used...