Smarty questions about the use of truncate

Source: Internet
Author: User
Smarty's usage of truncate {$ d. title | truncate: 25... the display is just a truncate. it seems that the numbers, various characters, and Chinese characters are treated as one character, but after all, two digits occupy the space of a Chinese character, and there is a "... ", occupies three more characters. This leads to uneven levels after titles are listed. The multi-digit display is very short, and the multi-digit display is very long. How to make truncate think of smarty's use of truncate
{$ D. title | truncate: 25}
In this way, I want to display excessive titles...

Just truncate seems to treat numbers, various characters, and Chinese as one character.
However, after all, the number occupies two Chinese spaces, and the "..." at the end occupies three characters.
This leads to uneven levels after titles are listed.

The multi-digit display is very short, and the multi-digit display is very long.
How can we make truncate think that the number is also half a character?

Hope you can advise me! thank you!

------ Solution --------------------
Why not handle this in PHP? Bangding
------ Solution --------------------
Define a function to intercept Chinese characters.
------ Solution --------------------
Alas, 20 points

Save the following code as modifier. mb_truncate.php and put this php in the libs/plugins directory under smarty.
Usage: {$ 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;    }} 

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.