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

About Truncate

 
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
ShenKong
Smarty Rookie


Joined: 20 Jan 2004
Posts: 27

PostPosted: Tue Jan 20, 2004 7:18 am    Post subject: About Truncate Reply with quote

Something Wrong When :

index.php:
[php:1:6b24aadce2]$smarty->assign("topic","这里是测试内容,看看是不是乱码");[/php:1:6b24aadce2]

index.tpl
Code:
Topic: {$topic|truncate:10:"..."}

if something can't display well , look at this picture :


The Output is :


This Bug may be fixed by this function , I think it is useful for you .
Cut words with PHP but not javascrypt .
[php:1:6b24aadce2] function cut_words($str, $strlen = 40, $other=true)
{
$str = stripslashes($str);
for($i = 0; $i < $strlen; $i++)
{
if(ord(substr($str, $i, 1))>0xa0)
{
$j++;
}
}
if($j % 2 != 0)
{
$strlen++;
}
$rstr = substr($str, 0, $strlen);
if(strlen($str) > $strlen && $other)
{
$rstr .= '...';
}
return $rstr;
}[/php:1:6b24aadce2]
_________________
Something comes soon ...
http://www.showtop.net
http://www.openphp.cn
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Apr 14, 2004 2:48 pm    Post subject: Reply with quote

right. truncate doesn't work with multibyte-strings. if you want to do me a favour, try this one:

[php:1:f4eccf4a6f]<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/


/**
* Smarty truncate modifier plugin
*
* Type: modifier<br>
* Name: mb_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.
*
* This version is mbstrings-aware. The correct encoding it
* should use has to be set with ini_set(
* 'mbstring.internal_encoding', ...); before using it.
*
* @link http://smarty.php.net/manual/en/language.modifier.truncate.php
* truncate (Smarty online manual)
* @param string
* @param integer
* @param string
* @param boolean
* @return string
*/
function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...',
$break_words = false)
{
if ($length == 0)
return '';

if (mb_strlen($string) > $length) {
$length -= mb_strlen($etc);
if (!$break_words)
$string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1));
return mb_substr($string, 0, $length).$etc;

} else
return $string;
}

?>[/php:1:f4eccf4a6f]

it's the current truncate but using the mb_*-string-function instead of the normal ones.

you have to set mbstring.internal_encoding to make it work properly.

if that works, the real truncate should also work if you set mbstring.func_overload correctly. or you can rename the modifier to "truncate" instead of "mb_truncate" and put it in a plugin-path that is searched before smarty's plugin_path.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Apr 14, 2004 5:30 pm    Post subject: Reply with quote

FYI,

php|a's cover story for April 2004 concerns using Smarty with internationalization including multibyte issues. I haven't read it yet, but it appears to cover this particular issue Smile
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