function utf8_trim($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 mul_excerpt ($excerpt,$excerpt_length) {
$myexcerpt = substr($excerpt,0,$excerpt_length);
return utf8_trim($myexcerpt) . ‘… ‘;
}
或者这样也可以
//自动截断不乱码
// jieduan luanma
/////////////////
function Limit_Char($max_char = 200, $more_text = '...', $more_link_text = '', $limit_type = 'content') {
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 ' '.$more_link_text.'';
}
} 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);
}
|