Simple string interception method that supports Chinese and other encodings, passing the corresponding parameters directly to the call can be
Copy Code code as follows:
/**
* String interception, support Chinese and other coding
* @static
* @access Public
* @param string $STR strings that need to be converted
* @param string $start start position
* @param string $length intercept length
* @param string $charset encoding format
* @param string $suffix truncate display characters
* @return String
*/
function Msubstr ($str, $start =0, $length, $charset = "Utf-8", $suffix =true) {
if (function_exists ("Mb_substr"))
$slice = Mb_substr ($str, $start, $length, $charset);
ElseIf (function_exists (' iconv_substr ')) {
$slice = Iconv_substr ($str, $start, $length, $charset);
if (false = = $slice) {
$slice = ';
}
}else{
$re [' utf-8 '] = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff] [\X80-\XBF] {3}/";
$re [' gb2312 '] = "/[\x01-\x7f]| [\xb0-\xf7] [\xa0-\xfe]/];
$re [' gbk '] = "/[\x01-\x7f]| [\x81-\xfe] [\x40-\xfe]/];
$re [' big5 '] = "/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe]) /";
Preg_match_all ($re [$charset], $STR, $match);
$slice = Join ("", Array_slice ($match [0], $start, $length));
}
Return $suffix? $slice. ' ... ': $slice;
}