wordpress 中文摘要截取乱码



//解决截取中文乱码
function Limit_Char($max_char = 200, $limiter='', $more_text = '...', $more_link_text = '') {
//if ($limit_type == 'title') { $limiter = get_the_title(); }
//else { $limiter = get_the_content(); }
//$limiter = apply_filters('the_content', $limiter);
//$limiter = strip_tags(str_replace(']]>', ']]>', $limiter));
if (strlen($limiter) > $max_char) {
$limiter = substr($limiter, 0, $max_char+1);
$limiter = utf8_conver($limiter);
echo $limiter;
echo $more_text;
if ($more_link_text != '') {
echo ' <a href="';
echo the_permalink();
echo '">'.$more_link_text.'</a>';
}
} else {
echo $limiter;
}
}

function utf8_conver($str) {
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}

把上述代码加入到function文件,然后调用

Limit_Char(80,'中文中文中文中文中文中文');