Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

truncate and utf-8

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
schildi
Smarty Regular


Joined: 11 Mar 2005
Posts: 81

PostPosted: Tue Jul 01, 2008 7:20 am    Post subject: truncate and utf-8 Reply with quote

truncate does not work correct with utf-8 encoded strings.

example:

{assign var="test" value="Ö-Umlaut"}
{$test|truncate:1:"":true|upper|rawurlencode}
{assign var="test" value="Ö"}
{$test|upper|rawurlencode}
{$test|rawurlencode}

display:

%C3
%C3%96
%C3%96

==> truncate cuts off the utf-8-encoded character.

please post your fix, as soon you have one. i tried the u-modifier within the preg_replace function, but thats not the cause in this case..
Back to top
View user's profile Send private message Send e-mail
schildi
Smarty Regular


Joined: 11 Mar 2005
Posts: 81

PostPosted: Tue Jul 01, 2008 7:36 am    Post subject: fast fix Reply with quote

just a fast fix, NOT seriously tested:

Code:

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty truncate modifier plugin
 *
 * Type:     modifier<br>
 * Name:     truncate<br>
 * 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 <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */

function smarty_utf8_substr($str,$from,$len){
# utf8 substr
# www.yeap.lv
  return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                       '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
                       '$1',$str);
}

function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                  $break_words = false, $middle = false)
{
    if ($length == 0)
        return '';

    if (strlen(utf8_decode($string)) > $length) {
        $length -= strlen(utf8_decode($etc));
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/u', '', smarty_utf8_substr($string, 0, $length+1));
        }
        if(!$middle) {
            return smarty_utf8_substr($string, 0, $length).$etc;
        } else {
            return smarty_utf8_substr($string, 0, $length/2) . $etc . smarty_utf8_substr($string, -$length/2);
        }
    } else {
        return $string;
    }
}

/* vim: set expandtab: */

?>
Back to top
View user's profile Send private message Send e-mail
Cybril
Smarty n00b


Joined: 03 Jul 2008
Posts: 1

PostPosted: Thu Jul 03, 2008 2:20 am    Post subject: Reply with quote

I can't get truncate to work at all - I get the error message 'unknown filter type truncate' - can anyone help?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP