<?php/** * String truncation (support Chinese) * @author: Zms * @version: 2011-7-27 *//** * String intercept * @param object $STR: String (Chinese supported) * @param object $start: Intercept start position * @param object $length: Intercept End Position * @param object $encode [OPTIONAL]: string encoding * @param object $input _encode [optional]: The encoding of the input text * @return */function substring ( $str, $start, $length, $encode = ' utf-8 ', $input _encode = ' utf-8 ') {//Encoding conversion $STR = Iconv ($input _encode , $encode, $STR); Regular matching Preg_match_all (Getrege ($encode), $str, $match); The data is obtained from the array, consisting of a string $slice = Join ("", Array_slice ($match [0], $start, $length)); return $slice; }/** * Chinese code * @param object $type * @return * * function Getrege ($type) {$re GE = ""; Switch (Strtolower ($type)) {case ' utf-8 ': $rege = "/[\x01-\x7f]|[ \xc2-\xdf][\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff][\x80-\xbf]{3}/"; Break Case ' gb2312 ': $rege = "/[\x01-\x7f]| [\xb0-\xf7] [\xa0-\xfe]/]; Break Case ' GBK ': $rege = "/[\x01-\x7f]| [\x81-\xfe] [\x40-\xfe]/]; Break Case "Big5": $rege = "/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe]) /"; Break Default:echo "CharSet error"; Exit } return $rege; }?>