: PHP character truncation solves the problem of Chinese character truncation without using the mb series: Copy the code as follows: functionCut_string ($ string, $ start, $ sublen, $ extstring ..., $ codeUTF-8) {Cut_string start if ($ codeUTF-8) {$ pa // character truncation to solve Chinese truncation problems
The code is as follows:
Function Cut_string ($ string, $ start, $ sublen, $ extstring = '... ', $ code = 'utf-8') {// starting with Cut_string 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 )). $ extstring; 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. = $ extstring; return $ tmpstr ;}// Cut_string ends
The above introduces the PHP character truncation to solve the Chinese character truncation problem, without the use of the mb series, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.