This is a Chinese Chinese character intercept function in the past few days because the project needs to be written:
# ========================================================================================================================================================================= =========================================================================================================================================================== ; # $ Fstart: From the first few Chinese characters, start to load, starting from the beginning to use 0 # $ flen: Intercept a few Chinese characters # $ fcode: The original string encoding method, default is GB2312 or BIG5, UTF-8 Press UTF -8 encoding method interception # -------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------- Function Msubstr ($ FSTR, $ FSTART, $ FLEN, $ FCODE = ") {Switch ($ fcode) {CASE "UTF-8": preg_match_all ("/ x01- / x7f] | [/ xc2- / xdf] [/ x80- / xbf] | / XE0 [/ xa0- / xbf] [/ x80- / xbf] | [/ x80- / xbf] [/ x80- / xbf] | / xf0 [/ x90- / xbf] [/ x80- / xbf] [/ x80- / xbf] | [/ xf1- / xf7] [/ x80- / xbf] [/ x80- / xbf] / ", $ fstr, $ ar); if (func_num_args ()> = 3 ) {IF ($ ar [0])> $ flen) {Return Join ("", Array_Slice ($ AR [0], $ FSTART, $ FLEN)). "...";} returnjin (" ", Array_Slice ($ AR [0], $ FSTART, $ FLEN);} else {returnjin (" ", array_slice ($ ar [0), $ fstart);} Break; Default: $ fstart = $ fstart * 2; $ flen = $ flen * 2; $ Strlen = Strlen ($ fstr); for ($ I = 0; $ I
$ flen) {IF (Substr ($ FSTR, $ I, 1))> 129) $ TMPSTR. = Substr ($ FSTR, $ I, 2); Else $ TMPSTR. = Substr ($ FSTR, $ I , 1);} IF (Substr ($ FSTR, $ I, 1))> 129) $ i ;} IF (Strlen ($ Tmpstr) $ TMPSTR. = "..."; Return $ Examples of use:}}?>
$ a = "One or two three or five six seven eight ninety"; $ b = iconv ("GB2312", "UTF-8", $ A); Echo "a =" .mstr ($ A, 0, 5). "
"; echo "b =" .mstr ($ B, 0, 5, "UTF-8"). "
";?>
The output results are "one or two three four five ...", if there is a Chinese and English mixed, there will be only a problem that only the Chinese character half-character characters.