Smarty about the use of truncate

Source: Internet
Author: User
Smarty questions about the use of truncate
{$d. title|truncate:25}
I would like to let the superfluous title with ... Show

It's just that truncate is like a number, a variety of characters, and Chinese as 1 characters.
But after all, the number accounted for 2 of a Chinese space, and there is the end of the "...", another 3 characters of space.
Causes the headings to be listed after the hierarchy is not aligned.

The number of the display is very short, more Chinese will show very long.
How do you make truncate think that numbers are half a character?

Hope master, veteran advice, thank you!

------Solution--------------------
Why not handle this in PHP? Help Top
------Solution--------------------
Customize functions that intercept Chinese.
------Solution--------------------
Alas, only 20 points.

Save the following code as modifier.mb_truncate.php and put this PHP in the Libs/plugins directory below Smarty
How to use: {$d. title|mb_truncate:25}


PHP Code
function Smarty_modifier_mb_truncate ($string, $length = A, $etc = ' ... ', $charset = ' UTF-8 ',                                  $break _words = False, $mi Ddle = 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;    
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.