您的位置:HBcms宏博内容管理系统 模板设计 正文
原作者:hbcms 添加时间:2008-07-30 原文发表:2006-07-30 人气:1708


hbcms模板应用增加了截取函数 cn_truncate

函数目的:
1。在模板中直接截取中文字符
2。可保留原来的字符串style,如 font 等


用法:
cn_truncate 用法大致同官方的 truncate 函数,如下:

<{$item_info.title|cn_truncate:18:"...":true}>

第1个参数 18 表示截取 18 个汉字

第2个参数 ... 表示,如果多余18个汉字,则显示 ...

第3个参数 true 表示保留文字的初始颜色。 false 表示去掉颜色。

已知的BUG:
只做到了保留第一个style,而且有BUG(可查考源代码),请高手完善。

本函数的最新完善版本可在  http://hbcms.com/cms/template/    中下载


源代码如下:

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://www.hbcms.com/
 * @author   http://www.hbcms.com/
 * @param string
 * @param integer
 * @param string
 * @param boolean $keep_first_style 是否保留第一个style
 * @return string

 * $keep_first_style 理解范例:

$s = '<A HREF="http://www.hbcms.com/"><FONT COLOR="red"><U><B>宏博<U>内容</U>管<B>理</B>系 &nbsp;统&nbsp;
<P>
<FONT SIZE="2" COLOR="blue">HBCMS</FONT> 是一个可以免费使用的内容管理系统,您甚至可以用<A HREF="http://www.hbcms.com/">她来做商业网站</A>。点这里查看详细版权说明

<BR>&nbsp;<BR>
是否有错误
<HR>
已知bug,截取:<FONT SIZE="2" COLOR="blue">乌木乌木乌<U>木乌木</U>乌木乌木乌木乌木乌木</font>有异常
<P>
希望大家改善</B></U></FONT></A>';
echo $s . '<hr><HR><HR>smarty_modifier_cn_truncate:<HR>';
echo smarty_modifier_cn_truncate($s,30,'。。。',1);
exit();
 */

function smarty_modifier_cn_truncate($string$strlen 20$etc '...',
                                  
$keep_first_style false)
{
    
$strlen $strlen*2;
    
$string trim($string);
    if ( 
strlen($string) <= $strlen )    {
        return 
$string;
    }
    
$str strip_tags($string);
    
$j 0;
    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  ) {$rstr .= $etc;} 

    if ( 
$keep_first_style == true && ereg('^<(.*)>$',$string) )    {
        if ( 
strlen($str) <= $strlen )    {
            return 
$string;
        }
        
$start_pos strpos($string,substr($str,0,4));
        
$end_pos strpos($string,substr($str,-4));
        
$end_pos $end_pos+4;
        
$rstr substr($string,0,$start_pos) . $rstr substr($string,$end_pos,strlen($string));
    }

    return 
$rstr

}

/* vim: set expandtab: */

?>

 

官方的函数是 truncate ,可惜只支持单字节的英文字符,用法如下:

Example 5-20. truncate

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

?>

where index.tpl is:

{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}

This will output:

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...

 

 

本页地址
相关文章

如何理解PHP模板引擎SMARTY
什么是Smaty?
Smarty是如何诞生的?Smarty模板引擎的来源
smarty官方文档下载
HBcms的模板语言是自己写的吗?学了有用吗?
征集模板:被采纳的模板将包含在HBcms的下一
模板的导入和导出功能介绍
HBcms的Smarty模板结构介绍
模板变量说明,如何制作个性模板
最基本的smarty变量:$web模板变量说明和使
自动显示导航条连接,解读$web.type模板变量
列表页模板变量$data_ary.main_article详解
Smarty中文手册,Smarty教程

相关评论

评论人:jones2007-06-10
very good!

本文章所属分类:首页 模板设计