Ticket #511 (closed bug: fixed)

Opened 5 months ago

Last modified 2 months ago

truncate modifier modification

Reported by: Torgan Assigned to:
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:
Documentation needed: 0 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

patch-#511.diff (2.0 kB) - added by bastnic on 06/13/08 18:38:08.

Change History

03/19/08 15:59:36 changed by laurentj

  • owner changed.
  • component changed from jelix to jelix:plugins.
  • severity changed from normal to minor.

Please provide a diff file for the trunk.

03/19/08 16:14:09 changed by bballizlife

Also don't forget to update the @contributor tag in the header of the file

04/15/08 08:58:54 changed by laurentj

  • milestone set to Jelix 1.1.

06/13/08 18:38:08 changed by bastnic

  • attachment patch-#511.diff added.

06/13/08 18:39:15 changed by bastnic

  • review set to review?.

Stupid to have some ticket like that still open, so there's the same patch with header changed and in the good format.

06/15/08 15:10:48 changed by laurentj

  • review changed from review? to review+.
  • milestone changed from Jelix 1.1 beta 1 to Jelix 1.0.5.

ok.

Should be landed in the 1.0.x branch and the trunk.

06/15/08 15:31:36 changed by bastnic

  • status changed from new to closed.
  • resolution set to fixed.

done.

Download in other formats: Comma-delimited Text Tab-delimited Text RSS Feed