From c8f22fe2472ffa47530cbdbe67574666818f2060 Mon Sep 17 00:00:00 2001 From: "Uwe.Tews" Date: Wed, 7 Jul 2010 16:34:06 +0000 Subject: [PATCH] - bugfix the truncate modifier needs to check if the string is utf-8 encoded or not --- change_log.txt | 3 + libs/plugins/modifier.truncate.php | 93 +++++++++++++++--------------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/change_log.txt b/change_log.txt index 574c0266..eb9ae648 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,6 @@ +07/07/2010 +- bugfix the truncate modifier needs to check if the string is utf-8 encoded or not + 06/07/2010 - create exception on recursive {extends} calls - fixed reported line number at "unexpected closing tag " exception diff --git a/libs/plugins/modifier.truncate.php b/libs/plugins/modifier.truncate.php index 0ad5f10f..af963074 100644 --- a/libs/plugins/modifier.truncate.php +++ b/libs/plugins/modifier.truncate.php @@ -1,29 +1,29 @@ -* 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 $string input string -* @param integer $length lenght of truncated text -* @param string $etc end string -* @param boolean $break_words truncate at word boundary -* @param boolean $middle truncate in the middle of text -* @return string truncated string -*/ + * 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 $string input string + * @param integer $length lenght of truncated text + * @param string $etc end string + * @param boolean $break_words truncate at word boundary + * @param boolean $middle truncate in the middle of text + * @return string truncated string + */ function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) { @@ -31,34 +31,37 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', return ''; if (is_callable('mb_strlen')) { - 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)); - } - if (!$middle) { - return mb_substr($string, 0, $length) . $etc; + if (mb_detect_encoding($text, 'UTF-8, ISO-8859-1') === 'UTF-8') { + // $string has utf-8 encoding + 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)); + } + if (!$middle) { + return mb_substr($string, 0, $length) . $etc; + } else { + return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2); + } } else { - return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2); + return $string; } + } + } + // $string has utf-8 no encoding + if (strlen($string) > $length) { + $length -= min($length, strlen($etc)); + if (!$break_words && !$middle) { + $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1)); + } + if (!$middle) { + return substr($string, 0, $length) . $etc; } else { - return $string; + return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2); } } else { - if (strlen($string) > $length) { - $length -= min($length, strlen($etc)); - if (!$break_words && !$middle) { - $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1)); - } - if (!$middle) { - return substr($string, 0, $length) . $etc; - } else { - return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2); - } - } else { - return $string; - } + return $string; } } -?> +?> \ No newline at end of file