1 /** 2 +---------------------------------------------------------- 3 * 字符串截取,支持中文和其他编码 4 +---------------------------------------------------------- 5 * @static 6 * @access public 7 +---------------------------------------------------------- 8 * @param string $str 需要转换的字符串 9 * @param string $start 开始位置 10 * @param string $length 截取长度 11 * @param string $charset 编码格式 12 * @param string $suffix 截断显示字符 13 +---------------------------------------------------------- 14 * @return string 15 +---------------------------------------------------------- 16 */ 17 function msubstr2($str, $start=0, $length, $suffix=true, $charset="utf-8") 18 { 19 if(function_exists("mb_substr")){ 20 if ($suffix && strlen($str)>$length) 21 return mb_substr($str, $start, $length, $charset)."..."; 22 else 23 return mb_substr($str, $start, $length, $charset); 24 } 25 elseif(function_exists('iconv_substr')) { 26 if ($suffix && strlen($str)>$length) 27 return iconv_substr($str,$start,$length,$charset)."..."; 28 else 29 return iconv_substr($str,$start,$length,$charset); 30 } 31 $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"; 32 $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/"; 33 $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/"; 34 $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/"; 35 preg_match_all($re[$charset], $str, $match); 36 $slice = join("",array_slice($match[0], $start, $length)); 37 if($suffix) return $slice."…"; 38 return $slice; 39 }