Auto: http://www.focuslib.com/article/php/1244913925.shtml
You can also use the mb_strimwidth function. However, both Chinese and English are counted as one character, and the output length is different ~
Code
Function substring ($ STR, $ start = 0, $ Len = 0, $ code = 'utf-8 '){
If (! Is_numeric ($ Len) return $ STR;
If ($ Len = 0) $ Len = strlen ($ Str)-$ start;
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, $ STR, $ tstr );
If (count ($ tstr [0])-$ Start> $ Len) return join ('', array_slice ($ tstr [0], $ start, $ Len )). "";
Return join ('', array_slice ($ tstr [0], $ start, $ Len ));
} Else {
$ Start = $ start * 2;
$ Len = $ Len * 2;
$ SL = strlen ($ Str );
$ Tmpstr = '';
For ($ I = 0; $ I <$ SL; $ I ++ ){
If ($ I >=$ start & $ I <($ start + $ Len )){
If (ord (substr ($ STR, $ I, 1) & gt; 129 ){
$ Tmpstr. = substr ($ STR, $ I, 2 );
} Else {
$ Tmpstr. = substr ($ STR, $ I, 1 );
}
}
If (ord (substr ($ STR, $ I, 1)> 129) $ I ++;
}
If (strlen ($ tmpstr) <$ SL) $ tmpstr. = '';
Return $ tmpstr;
}
}