It is estimated that many children who play with smarty templates have encountered the problem of cropping garbled characters. Especially UTF8 encoded.
Save the following code as modifier.truncate2.php and save it to the plugin directory under smarty libs
Then use $v->content|truncate2:100
when cropping
That’s it.
If it does not work, it may be caused by cache. Please delete the cache file under templates_c as soon as possible (the editor encountered a cache problem when doing it.)
Copy code The code is as follows:
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty truncate modifier plugin
*
* Type: modifier
* Name: truncate
* 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
* @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('00000000'.$a, -8);
if ($n < $start) {
if (substr($a, 0, 1) == 0) {
elseif (substr($a, 0, 3) == 110) {
+= 2;
[] = substr($ str, $i, 1);
}elseif (substr($a, 0, 3) == 110) {
$i += 1;
+= 2;
break;
}
}
}
return $return_ary ? $r : implode("",$r);
}
/* vim: set expandtab: */
? >
Samrty’s plug-in system is relatively smart and easy to modify.
http://www.bkjia.com/PHPjc/802225.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/802225.htmlTechArticleIt is estimated that many children who play smarty templates have encountered the problem of cropping garbled characters. Especially UTF8 encoded. Save the following code as modifier.truncate2.php and save it to the plugin directory under smarty libs...
Statement: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