一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress页面截取中文乱码解决办法

时间:2013-08-07 编辑:简简单单 来源:一聚教程网

今天提到的程序中截取中文乱码的问题尤其是在首页,对人的第一印象是非常不友好的,现将页面乱码问题的解决方案分享以下所述。

 代码如下 复制代码

echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 330,"...");
 ?>   

 以上是wordpress中的源代码,这段代码没有处理好中文的截取问题(老外的开源东西竟然没有考虑到china这个大用户),解决方案如下:

 代码如下 复制代码
echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 330,"...", "utf-8");
?>    

 

中涉及到的php文件有:archive.php  category.php  index.php  sidebar-category.php  sidebar-single.php(文件在外观–>编辑里面)

给你一个php中文截取函数

 代码如下 复制代码

function SubTitle($String,$Length) {
if (mb_strwidth($String, 'UTF8') <= $Length ){
return $String;
}else{
$I = 0;
$len_word = 0;
while ($len_word < $Length){
$StringTMP = substr($String,$I,1);
if ( ord($StringTMP) >=224 ){
$StringTMP = substr($String,$I,3);
$I = $I + 3;
$len_word = $len_word + 2;
}elseif( ord($StringTMP) >=192 ){
$StringTMP = substr($String,$I,2);
$I = $I + 2;
$len_word = $len_word + 2;
}else{
$I = $I + 1;
$len_word = $len_word + 1;
}
$StringLast[] = $StringTMP;
}
/* raywang edit it for dirk for (es/index.php)*/
if (is_array($StringLast) && !empty($StringLast)){
$StringLast = implode("",$StringLast);
$StringLast .= "...";
}
return $StringLast;
}
}
 

热门栏目