/**
* 字符截取 支持UTF8/GBK
* @param $string
* @param $length
* @param $dot
*/
function str_cut($string, $length, $dot = '...') {
$strlen = strlen($string);
if($strlen <= $length) return $string;
$string = str_replace(array(' ',' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
$strcut = '';
if(strtolower(CHARSET) == 'utf-8') {
$length = intval($length-strlen($dot)-$length/);
$n = $tn = $noc = ;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == || $t == || ( <= $t && $t <= )) {
$tn = ; $n++; $noc++;
} elseif( <= $t && $t <= ) {
$tn = ; $n += ; $noc += ;
} elseif( <= $t && $t <= ) {
$tn = ; $n += ; $noc += ;
} elseif( <= $t && $t <= ) {
$tn = ; $n += ; $noc += ;
} elseif( <= $t && $t <= ) {
$tn = ; $n += ; $noc += ;
} elseif($t == || $t == ) {
$tn = ; $n += ; $noc += ;
} else {
$n++;
}
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, , $n);
$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), $strcut);
} else {
$dotlen = strlen($dot);
$maxi = $length - $dotlen - ;
$current_str = '';
$search_arr = array('&',' ', '"', "'", '“', '”', '—', '<', '>', '·', '…','∵');
$replace_arr = array('&',' ', '"', ''', '“', '”', '—', '<', '>', '·', '…',' ');
$search_flip = array_flip($search_arr);
for ($i = ; $i < $maxi; $i++) {
$current_str = ord($string[$i]) > ? $string[$i].$string[++$i] : $string[$i];
if (in_array($current_str, $search_arr)) {
$key = $search_flip[$current_str];
$current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
}
$strcut .= $current_str;
}
}
return $strcut.$dot;
}