Utf-8 format text in the string interception is prone to a section of the case, and prone to garbled, this is a relatively simple function, requires PHP to open the mbstring extension
$text is the text to be intercepted, $length is the length of the interception, $suffix indicates whether to intercept the following ellipsis
/** * string interception, support for Chinese and other encodings * @static * @access public * @param string $str strings to be converted * @param string $start Start position * @param string $length intercept length * @param string $charset encoded format * @param string $suffix truncate display character * @return s Tring */static Public function Msubstr ($STR, $start =0, $length, $charset = "Utf-8", $suffix =true) {if (functi On_exists ("Mb_substr")) $slice = Mb_substr ($str, $start, $length, $charset); ElseIf (function_exists (' iconv_substr ')) {$slice = Iconv_substr ($str, $start, $length, $charset); }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; }