Php smarty truncate UTF8 garbled Problem Solution

Source: Internet
Author: User

It is estimated that many of the children playing with the smarty template have encountered cropping garbled characters. Especially UTF-8 encoding.

Run the following code to save modifier. truncate2.php to the plugin directory under smarty libs:

Use $ v-> content | truncate2: 100 when cropping.

That's it.

If it is not easy to use, it may be caused by caching. please delete the cached files under templates_c at a high speed (Cache problems may occur during the compilation process .)
Copy codeThe Code is as follows:
<? Php
/**
* Smarty plugin
* @ Package Smarty
* @ Subpackage plugins
*/

 
/**
* Smarty truncate modifier plugin
*
* Type: modifier <br>
* Name: truncate <br>
* Purpose: Truncate a string to a certain length if necessary,
* Optionally splitting in the middle of a word, and
* Appending the $ etc string or inserting $ etc into the middle.
* @ Link http://smarty.php.net/manual/en/language.modifier.truncate.php
* Truncate (Smarty online manual)
* @ Author Monte Ohrt <monte at ohrt dot com>
* @ Param string
* @ Param integer
* @ Param string
* @ Param boolean
* @ Param boolean
* @ Return string
*/
Function smarty_modifier_truncate2 ($ string, $ length = 80, $ etc = '...', $ count_words = true ){
Return $ returnstr = substr_utf8 ($ string, 0, $ length). $ etc;

}

Function substr_utf8 ($ str, $ start = 0, $ length =-1, $ return_ary = false ){
$ Len = strlen ($ str); if ($ length =-1) $ length = $ len;
$ R = array ();
$ N = 0;
$ M = 0;

For ($ I = 0; $ I <$ len; $ I ++ ){
$ X = substr ($ str, $ I, 1 );
$ A = base_convert (ord ($ x), 10, 2 );
$ A = substr ('20140901'. $ a,-8 );
If ($ n <$ start ){
If (substr ($ a, 0, 1) = 0 ){
} Elseif (substr ($ a, 0, 3) = 110 ){
$ I + = 1;
} Elseif (substr ($ a, 0, 4) = 1110 ){
$ I + = 2;
}
$ N ++;
} Else {
If (substr ($ a, 0, 1) = 0 ){
$ R [] = substr ($ str, $ I, 1 );
} Elseif (substr ($ a, 0, 3) = 110 ){
$ R [] = substr ($ str, $ I, 2 );
$ I + = 1;
} Elseif (substr ($ a, 0, 4) = 1110 ){
$ R [] = substr ($ str, $ I, 3 );
$ I + = 2;
} Else {
$ R [] = '';
}
If (++ $ m >=$ length ){
Break;
}
}
}

Return $ return_ary? $ R: implode ("", $ r );
}
/* Vim: set expandtab :*/
?>
Samrty's plug-in system is intelligent and easy to modify.

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.