Ticket #511 (closed bug: fixed)
truncate modifier modification
| Reported by: | Torgan | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Jelix 1.0.5 |
| Component: | jelix:plugins | Version: | 1.0.2 |
| Severity: | minor | Keywords: | template modifier truncate |
| Cc: | Php version: | ||
| Review: | review+ | Hosting Provider: | |
| Blocked By: | Documentation needed: | no | |
| Blocking: |
Description
the truncate modifier uses the iconv functions that sometimes hangs and generate "memory exhausted errors" when addressing UTF8 texts. mb_ seems more stable, though less universally present on php server.
This patch makes the template modifier truncate use mb_ functions if available, or iconv functions if not:
<?php
/* comments & extra-whitespaces have been removed by jBuildTools*/
/**
* Plugin from smarty project and adapted for jtpl
* @package jelix
* @subpackage jtpl_plugin
* @author
* @contributor Laurent Jouanneau (utf8 compliance)
* @copyright 2001-2003 ispi of Lincoln, Inc., 2007 Laurent Jouanneau
* @link http://smarty.php.net/
* @link http://jelix.org/
* @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
*/
function jtpl_modifier_common_truncate($string, $length = 80, $etc = '...',
$break_words = false)
{
if (function_exists ('mb_strlen'))
{
$f_strlen = 'mb_strlen';
}
else
{
$f_strlen = 'iconv_strlen';
}
if (function_exists ('mb_substr'))
{
$f_substr = 'mb_substr';
}
else
{
$f_substr = 'iconv_substr';
}
if($length == 0)
return '';
$charset = jTpl::getEncoding();
if($f_strlen ($string,$charset) > $length){
$length -= $f_strlen($etc,$charset);
if(!$break_words)
$string = preg_replace('/\s+?(\S+)?$/', '', $f_substr($string, 0, $length+1,$charset));
return $f_substr($string, 0, $length,$charset).$etc;
} else
return $string;
}
?>
Attachments
Change History
Note: See
TracTickets for help on using
tickets.
