今天提到的程序中截取中文亂碼的問題尤其是在首頁,對人的第一印象是非常不友好的,現將頁面亂碼問題的解決方案分享以下所述,實例代碼如下:
- <?php
- echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 330,"...");
- ?>
以上是wordpress中的源代碼,這段代碼沒有處理好中文的截取問題(老外的開源東西竟然沒有考慮到china這個大用戶).解決方案如下:
- <?php
- 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) && !emptyempty($StringLast)){
- $StringLast = implode("",$StringLast);
- $StringLast .= "...";
- }
- return $StringLast;
- }
- }
新聞熱點
疑難解答
圖片精選