PHP implements non-garbled truncation of Chinese characters. the built-in substr () function of PHP cannot perform a good truncation of Chinese characters, and garbled characters may occur for some Chinese and English characters. The following two solutions are provided .? 1. what is the truncation of GB2312 encoding? Functionmsubstr ($ st PHP implements non-garbled truncation of Chinese characters
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
?
Function msubstr ($ str, $ start, $ len) {if (strlen ($ str)-$ start <$ len) return false; $ tmpstr = ""; $ strlen = $ start + $ len; for ($ I = 0; $ I <$ strlen; $ I ++) {if (ord (substr ($ str, $ I, 1)> 0xa0) {// 0xa0 indicates that the ASCII value of the first character in Chinese character encoding is greater than 0xa0 $ tmpstr. = substr ($ str, $ I, 2); $ I ++;} else $ tmpstr. = substr ($ str, $ I, 1);} return $ tmpstr. "... ";}
?
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 ).
?
// $ Sourcestr is the string to be processed // $ cutlength is the truncated length (that is, the number of words) function cut_str ($ sourcestr, $ cutlength) {$ returnstr = ''; $ I = 0; $ n = 0; $ str_length = strlen ($ sourcestr); // string bytes while ($ n <$ cutlength) and ($ I <= $ str_length) {$ temp_str = substr ($ sourcestr, $ I, 1); $ ascnum = Ord ($ temp_str ); // Obtain the ascii code of the $ I character in the string if ($ ascnum> = 224) // if the ASCII bit height is 224, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 3); // count three consecutive characters as a single character according to the UTF-8 encoding specification $ I = $ I + 3; // The actual bytes are calculated as 3 $ n ++; // string length sensor 1} elseif ($ ascnum> = 192) // if the ASCII bit height is 192, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 2); // Count 2 consecutive characters as a single character according to the UTF-8 encoding specification $ I = $ I + 2; // The actual Byte is calculated as 2 $ n ++; // string length gauge 1} elseif ($ ascnum> = 65 & $ ascnum <= 90) // if it is a capital letter, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 1); $ I = $ I + 1; // The actual number of bytes is still 1 $ n ++; // but consider the overall appearance. uppercase letters are counted as a high character} else // In other cases, including lowercase letters and halfwidth punctuation marks, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 1); $ I = $ I + 1; // The actual number of bytes is 1 $ n = $ n + 0.5; // lower-case letters, Halfwidth punctuation marks, and half-width upper-limit characters ...}} if ($ str_length> $ cutlength) {$ returnstr = $ returnstr. "... "; // when the length is exceeded, add a ellipsis at the end} return $ returnstr ;}?
?
?
?
The first floor zeroneta haha that I'm in a UTF-8 without garbled
Function utf8 ($ a, $ s = '')
{
Preg_match_all ('/[\ 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]/', $ a, $ d, PREG_PATTERN_ORDER );
Return join ($ s, $ d [0]);
} Method of calling zeroneta on the second floor
Utf8 (substr ('intercept me ', 0, 1); bupt_roy 2011-09-29 zeroneta on the third floor wrote the call method
Utf8 (substr ('intercept me', 0, 1 ));
Learn, learn, haha