Home > Backend Development > PHP Tutorial > smarty 关于truncate使用的有关问题

smarty 关于truncate使用的有关问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 13:30:03
Original
920 people have browsed it

smarty 关于truncate使用的问题
{$d.title|truncate:25}
我想这样让多余的标题以...显示

只是truncate好像是把数字、各种字符、中文一样当做1个字符
但数字毕竟占2个才占一个中文的空间,而且有末尾的“...”,又占了3个字符的空间。
导致标题列出后层次不齐。

数字多的显示很短,中文多的又会显示很长。
如何让truncate认为数字也是半个字符呢?

希望高手、老鸟指教,多谢!

------解决方案--------------------
为什么不在PHP里处理这些呢?帮顶
------解决方案--------------------
自定义截取中文的函数。
------解决方案--------------------
唉 才20分

将以下code保存为modifier.mb_truncate.php,将此php放入smarty下面的libs/plugins目录下
使用方法:{$d.title|mb_truncate:25}


PHP code

function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...', $charset='UTF-8',
                                  $break_words = false, $middle = false)
{
    if ($length == 0)
        return '';
 
    if (mb_strlen($string) > $length) {
        $length -= min($length, mb_strlen($etc));
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length+1, $charset));
        }
        if(!$middle) {
            return mb_substr($string, 0, $length, $charset) . $etc;
        } else {
            return mb_substr($string, 0, $length/2, $charset) . $etc . mb_substr($string, -$length/2, (mb_strlen($string)-$length/2), $charset);
        }
    } else {
        return $string;
    }
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template