1 <? PHP 2 function mymbsubstr ($ STR, $ start, $ length, $ charset) {3 $ charsets ["UTF-8"] = $ charsets ["utf8"] = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ X80 -\ xbf] | [\ xe0-\ XeF] [\ X80-\ xbf] {2} | [\ xf0-\ xFF] [\ X80-\ xbf] {3 }/"; 4 $ charsets ["gb2312"] = "/[\ x01-\ x7f] | [\ xb0-\ xf7] [\ xa0-\ xfe]/"; 5 $ charsets ["GBK"] = "/[\ x01-\ x7f] | [\ x81-\ xfe] [\ X40-\ xfe]/"; 6 $ charsets ["big5"] = "/[\ x01-\ x7f] | [\ x81-\ xfe] ([\ X40-\ x7e] | \ xA1-\ xfe ]) /"; 7 8 PR Eg_match_all ($ charsets [$ charset], $ STR, $ matches); # split the string into an array 9 10 # var_dump ($ matches) according to the specified encoding ); 11 12 $ substr = implode ("", array_slice ($ matches [0], $ start, $ length); # obtain the sub-array of the array, and connect the elements of the sub-array 13 14 return $ substr; 15} 16 17 $ STR = "I'm a coders"; 18 19 echo "<br> ". mymbsubstr ($ STR, 0, 7, "utf8 "). "<br>"; 20 echo mb_substr ($ STR, 0, 7, "utf8"); 21?>
Output
Array (1) {[0] => array (8) {[0] => string (1) "I" [1] => string (1) "'" [2] => string (1) "M" [3] => string (1) "" [4] => string (1) "A" [5] => string (1) "" [6] => string (3) "code" [7] => string (3) "Nong "}}
I'm a code
I'm a code