zoukankan      html  css  js  c++  java
  • 【转】SMARTY 不乱码截取中文

    <?php
    /*SMARTY 不乱码截取中文

    使用方法:把文件命名为:modifier.cn_cut.php 放在smarty下的plugins目录下,第二个参数是如果有截掉部分,那么后面会加后缀,第三个参数是页面的charset.
    {$string|cn_cut:10:'...'} 第一个参数是必填,其他是可选.
    */
    function smarty_modifier_cn_cut($string, $length = 80, $etc = '', $code = 'UTF-8'){
        if ($length == 0)
            return '';
        if ($code == 'UTF-8') {
            $pa = "/[\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]/";
        }else {
        $pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
        }
        preg_match_all($pa, $string, $t_string);
        if (count($t_string[0]) > $length)
            return join('', array_slice($t_string[0], 0, $length)) . $etc;
        return join('', array_slice($t_string[0], 0, $length));
    }
    ?>

  • 相关阅读:
    数据库迁移到Amazon RDS 的问题
    排序算法之希尔排序
    第一个 Shell脚本
    排序算法之直接插入排序
    当前工作参考
    cerr
    阿里云典型应用案例
    云服务引擎ACE
    阿里云SLB
    指针使用注意事项
  • 原文地址:https://www.cnblogs.com/kiter/p/2965680.html
Copyright © 2011-2022 走看看