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.