Ticket #511 (closed bug: fixed)

Opened 2 years ago

Last modified 21 months ago

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

patch-#511.diff (2.0 kB) - added by bastnic 21 months ago.

Change History

Changed 2 years ago by laurentj

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

Please provide a diff file for the trunk.

Changed 2 years ago by bballizlife

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

Changed 2 years ago by laurentj

  • milestone set to Jelix 1.1

Changed 21 months ago by bastnic

Changed 21 months ago 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.

Changed 21 months ago 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.

Changed 21 months ago by bastnic

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

done.

Note: See TracTickets for help on using tickets.