zoukankan      html  css  js  c++  java
  • smarty模板截取字符串乱码问题完美解决```````

    每一次遇到问题,都有解决的方法。

    当你解决了问题时,会发现原来自己一直在进步````

    学php不久,却喜欢上了这种语言。太完美了。

    今天遇到了一个乱码问题,这类问题对于一个smarty模板忠实者来说,应该算是普遍性的。

    个人认为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}

    就这么简单```

  • 相关阅读:
    Unity3d中的属性(Attributes)整理【转载】
    在Unity里面使MMD动起来吧【2】
    在Unity里面使MMD动起来吧【1】
    Unity代码混淆方案
    萌舞Android版Unity笔记整理
    c#实现16进制和字符串之间转换的代码
    关于sqlite3使用top的一些规则总结
    【转】UniSky入门资料
    【转】.NET 产品版权保护方案 (.NET源码加密保护)
    百度云管家下载出错解决思路记录
  • 原文地址:https://www.cnblogs.com/fengju/p/6174135.html
Copyright © 2011-2022 走看看