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

plugin truncate function request modify!

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
TyroneYeh
Smarty n00b


Joined: 30 May 2009
Posts: 1

PostPosted: Sat May 30, 2009 6:15 pm    Post subject: plugin truncate function request modify! Reply with quote

add mb_substr and mb_strlen can support Unicode char!!!

Thanks

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

    if (mb_strlen($string) > $length) {
        $length -= min($length, mb_strlen($etc));
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1));
        }
        if(!$middle) {
            return mb_substr($string, 0, $length) . $etc;
        } else {
            return mb_substr($string, 0, $length/2) . $etc . mb_substr($string, -$length/2);
        }
    } else {
        return $string;
    }
}
Back to top
View user's profile Send private message
nothinghood
Smarty Rookie


Joined: 20 Aug 2009
Posts: 5

PostPosted: Fri Aug 21, 2009 7:15 am    Post subject: Reply with quote

if $middle = true drops the end of the string this can help:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=8043&highlight=truncate

bye
nh
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 -> Feature Requests 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