smarty truncate
截取字符串
从字符串开始处截取某长度的字符,默认的长度为80指定第二个参数作为截取字符串的长度默认情况下,smarty会截取到一个词的末尾,如果需要精确到截取多少个字符可以使用第三个参数,将其设为”true”具体用法如下:复制代码 代码如下://index.php $smarty = new
Smarty; $smarty->assign(‘articleTitle’, ‘Two Sisters Reunite after
Eighteen Years at Checkout Counter.’); $smarty->display; //index.tpl
{$articleTitle} {$articleTitle|truncate} {$articleTitle|truncate:30}
{$articleTitle|truncate:30:””} {$articleTitle|truncate:30:”—“}
{$articleTitle|truncate:30:””:true}{$articleTitle|truncate:30:”…”:true}输出结果:Two
Sisters Reunite after Eighteen Years at Checkout Counter.Two Sisters
Reunite after Eighteen Years at Checkout Counter.Two Sisters Reunite
after…Two Sisters Reunite afterTwo Sisters Reunite after—Two Sisters
Reunite after EighTwo Sisters Reunite after E…

具体用法如下:

 

 代码如下

$smarty = new Smarty;
$smarty->assign(‘articleTitle’, ‘Two Sisters Reunite after Eighteen
Years at Checkout Counter.’);
$smarty->display(‘index.tpl’);

//index.php
$smarty = new Smarty;
$smarty->assign(‘articleTitle’, ‘Two Sisters Reunite after Eighteen
Years at Checkout Counter.’);
$smarty->display(‘index.tpl’);
//index.tpl
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:””}
{$articleTitle|truncate:30:”—“}
{$articleTitle|truncate:30:””:true}
{$articleTitle|truncate:30:”…”:true}

index.tpl:

输出结果:

{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:””}
{$articleTitle|truncate:30:”—“}
{$articleTitle|truncate:30:””:true}
{$articleTitle|truncate:30:”…”:true}

Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after…
Two Sisters Reunite after
Two Sisters Reunite after—
Two Sisters Reunite after Eigh
Two Sisters Reunite after E…

OUTPUT:

如果上面是英文肯定没有问题,但是出现中文好像就是乱码了,因为Smarty的truncate截取的是字符(占一个字节),

Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after…
Two Sisters Reunite after
Two Sisters Reunite after—
Two Sisters Reunite after Eigh
Two Sisters Reunite after E…

但是如果是中文,例如UTF-8(占3个字节),那么在截取的时候这里的参数11是字节数,如果是中文,则它实际上是截

取3个汉字(9个字节),剩下的2字节不能表示一个汉字,那么它就会以乱码的形式显示出来

解决办法

 代码如下

<?php
function smarty_modifier_truncate_utf($string, $length = 80, $etc =
‘…’)
{
 $result = ”;
 $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES,
‘utf-8’);
 for($i = 0, $j = 0; $i < strlen($string); $i++)
 {
  if($j >= $length)
  {
   for($x = 0, $y = 0; $x < strlen($etc); $x++)
   {
    if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8,
‘0’, STR_PAD_LEFT), ‘0’))
    {
     $x += $number – 1;
     $y++;
    }
    else
    {
     $y += 0.5;
    }
   }
   $length -= $y;
   break;
  }
  if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8,
‘0’, STR_PAD_LEFT), ‘0’))
  {
   $i += $number – 1;
   $j++;
  }
  else
  {
   $j += 0.5;
  }
 }
 for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++)
 {
  if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8,
‘0’, STR_PAD_LEFT), ‘0’))
  {
   if($length < 1.0)
   {
    break;
   }
   $result .= substr($string, $i, $number);
   $length -= 1.0;
   $i += $number – 1;
  }
  else
  {
   $result .= substr($string, $i, 1);
   $length -= 0.5;
  }
 }
 $result = htmlentities($result, ENT_QUOTES, ‘utf-8’);
 if($i < strlen($string))
 {
  $result .= $etc;
 }
 return $result;
}
?>

修改smarty内容函数即可了,大家可参考。

相关文章