这个Wordpress博客为了内部链接的优化,安装了桑林志汉化过的Random Posts插件,安装后发现在IE下查看源代码是乱码,而在firefox下则显示正常,仔细查看了一下插件的代码,发现原来是截字惹的祸。
这个插件为每个链接title默认截取400个字符,如:
根据UTF-8编码规范,一般将3个连续的字符计为单个字符,400肯定不能被3整除了,所以截取的最后字符就出现了乱码,使整个html源码就出现了乱码,所以不能简单地用substr这个php函数,得重新写一个截取字符函数,下面是我改正过的Random Posts源文件:
/*
Plugin Name: Random Posts for Chinese
Plugin URI: http://yanfeng.org/blog/index.php?p=373
Description: Displays a configurable list of random posts. Originally written by Alexander Malov, modified to work in languages like Chinese
Version: 1.0
Author: Mulberry
Author URI: http://yanfeng.org/
*/
function random_posts ($limit = 5, $length = 400, $before = ‘
‘, $show_pass_post = false, $show_excerpt_in_title = true) {
global $wpdb, $tableposts;
$sql = “SELECT ID, post_title, post_date, post_content FROM $tableposts WHERE post_status = ‘publish’ “;
if(!$show_pass_post) $sql .= “AND post_password =” “;
$sql .= “ORDER BY RAND() LIMIT $limit”;
$posts = $wpdb->get_results($sql);
$output = ”;
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$post_date = mysql2date(’j.m.Y’, $post->post_date);
$permalink = get_permalink($post->ID);
$post_content = strip_tags($post->post_content);
$post_content = stripslashes($post_content);
$post_strip = cut_str($post_content,$length);
$post_strip = str_replace(’”‘, ”, $post_strip);
$output .= $before . ‘
}
$output .= $post_date . '">‘ . $post_title . ‘‘;
if(!$show_excerpt_in_title) {
$output .= ‘: ‘ . $post_strip . ‘… ‘;
}
$output .= $after;
}
echo $output;
}
//$sourcestr 是要处理的字符串
//$cutlength 为截取的长度(即字数)
function cut_str($sourcestr,$cutlength)
{
$returnstr=”;
$i=0;
$n=0;
$str_length=strlen($sourcestr);//字符串的字节数
while (($n<$cutlength) and ($i<=$str_length))
{
$temp_str=substr($sourcestr,$i,1);
$ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
if ($ascnum>=224) //如果ASCII位高与224,
{
$returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
$i=$i+3; //实际Byte计为3
$n++; //字串长度计1
}
elseif ($ascnum>=192) //如果ASCII位高与192,
{
$returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
$i=$i+2; //实际Byte计为2
$n++; //字串长度计1
}
elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,
{
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //实际的Byte数仍计1个
$n++; //但考虑整体美观,大写字母计成一个高位字符
}
else //其他情况下,包括小写字母和半角标点符号,
{
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //实际的Byte数计1个
$n=$n+0.5; //小写字母和半角标点等与半个高位字符宽...
}
}
if ($str_length>$cutlength*3){
$returnstr = $returnstr . “…”;//超过长度时在尾处加上省略号
}
return $returnstr;
}
?>
Leave a comment
Fields in bold are required. Email addresses are never published or distributed.
Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>URIs must be fully qualified (eg: http://www.dupola.com) and all tags must be properly closed.
Line breaks and paragraphs are automatically converted.
Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.