- bugfix the truncate modifier needs to check if the string is utf-8 encoded or not

This commit is contained in:
Uwe.Tews
2010-07-07 16:34:06 +00:00
parent c83ef6792c
commit c8f22fe247
2 changed files with 51 additions and 45 deletions

View File

@@ -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 06/07/2010
- create exception on recursive {extends} calls - create exception on recursive {extends} calls
- fixed reported line number at "unexpected closing tag " exception - fixed reported line number at "unexpected closing tag " exception

View File

@@ -1,29 +1,29 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* *
* @package Smarty * @package Smarty
* @subpackage PluginsModifier * @subpackage PluginsModifier
*/ */
/** /**
* Smarty truncate modifier plugin * Smarty truncate modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: truncate<br> * Name: truncate<br>
* Purpose: Truncate a string to a certain length if necessary, * Purpose: Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word, and * optionally splitting in the middle of a word, and
* appending the $etc string or inserting $etc into the middle. * 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) * @link http://smarty.php.net/manual/en/language.modifier.truncate.php truncate (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string $string input string * @param string $string input string
* @param integer $length lenght of truncated text * @param integer $length lenght of truncated text
* @param string $etc end string * @param string $etc end string
* @param boolean $break_words truncate at word boundary * @param boolean $break_words truncate at word boundary
* @param boolean $middle truncate in the middle of text * @param boolean $middle truncate in the middle of text
* @return string truncated string * @return string truncated string
*/ */
function smarty_modifier_truncate($string, $length = 80, $etc = '...', function smarty_modifier_truncate($string, $length = 80, $etc = '...',
$break_words = false, $middle = false) $break_words = false, $middle = false)
{ {
@@ -31,34 +31,37 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...',
return ''; return '';
if (is_callable('mb_strlen')) { if (is_callable('mb_strlen')) {
if (mb_strlen($string) > $length) { if (mb_detect_encoding($text, 'UTF-8, ISO-8859-1') === 'UTF-8') {
$length -= min($length, mb_strlen($etc)); // $string has utf-8 encoding
if (!$break_words && !$middle) { if (mb_strlen($string) > $length) {
$string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1)); $length -= min($length, mb_strlen($etc));
} if (!$break_words && !$middle) {
if (!$middle) { $string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1));
return mb_substr($string, 0, $length) . $etc; }
if (!$middle) {
return mb_substr($string, 0, $length) . $etc;
} else {
return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2);
}
} else { } 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 { } else {
return $string; return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
} }
} else { } else {
if (strlen($string) > $length) { return $string;
$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;
}
} }
} }
?> ?>