/** * 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_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) { if ($length == 0) { return ''; } $ansi_as = 2; if (strlen_ansi($string, Smarty::$_CHARSET, $ansi_as) > $length) { $length -= min($length, strlen_ansi($etc, Smarty::$_CHARSET, $ansi_as)); if (!$break_words && !$middle) { $string = preg_replace('/\\s+?(\\S+)?$/', '', substr_ansi($string, 0, $length + 1, Smarty::$_CHARSET, $ansi_as)); } if (!$middle) { return substr_ansi($string, 0, $length, Smarty::$_CHARSET, $ansi_as) . $etc; } else { return substr_ansi($string, 0, $length / 2, Smarty::$_CHARSET, $ansi_as) . $etc . substr_ansi($string, -$length / 2, 0, Smarty::$_CHARSET, $ansi_as); } } else { return $string; } }
/** * Get the size of an attribute. * * @param string $attribute * @param mixed $value * @return mixed */ protected function getSize($attribute, $value) { $hasNumeric = $this->hasRule($attribute, $this->numericRules); // This method will determine if the attribute is a number, string, or file and // return the proper size accordingly. If it is a number, then number itself // is the size. If it is a file, we take kilobytes, and for a string the // entire length of the string will be considered the attribute size. if (is_numeric($value) && $hasNumeric) { return array_get($this->data, $attribute); } elseif (is_array($value)) { return count($value); } elseif ($value instanceof File) { return $value->getSize() / 1024; } //宽字节按照字体的几个宽度计算,比如「微软雅黑」下,汉字占据两个显示宽度 $rule = $this->getRule($attribute, 'Ansi'); $ansiWidth = empty($rule) || empty($rule[1]) ? 1 : intval($rule[1][0]); return strlen_ansi($value, NULL, $ansiWidth); //return mb_strlen($value); }