全球主机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 789|回复: 1

smarty truncate截取中文出现乱码

[复制链接]
发表于 2014-11-18 11:13:14 | 显示全部楼层 |阅读模式

个人认为smarty和php是个近乎完美的组合。



下面就是解决问题的方法:


smarty模板截取字符串后边几个字乱码。


改写文件modifier_truncate.php


function smarty_modifier_truncatey( $string,$length = 80,$etc='...',$count_words = true ) {
        mb_internal_encoding("UTF-8");
        if ($length == 0)return '';
        if ( strlen( $string ) <= $length ) return $string;
        preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
   if( $count_words ){
                $j = 0;
                for($i=0; $i<count($info[0]); $i++) {
                        $wordscut .= $info[0][$i];
                        if( ord( $info[0][$i] ) >=128 ){
                                $j = $j+2;
                        }else{
                                $j = $j + 1;
                        }
                        if ($j >= $length ) {
                                        return $wordscut.$etc;
                        }
                }
                return join('', $info[0]);
   }
        return join("",array_slice( $info[0],0,$length ) ).$etc;
}


读取{$xnews[t].title|truncate:14}

发表于 2015-11-6 09:47:05 | 显示全部楼层
说的好,一定回复~











于海滨3d预测 neiba.cn/
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|小黑屋|全球服务器论坛

GMT+8, 2024-4-18 12:37 , Processed in 2.812161 second(s), 18 queries , File On.

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表