解析smarty 截取字符串函数 truncate的用法介绍

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');
//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…

(0)

相关推荐

  • Smarty中常用变量操作符汇总

    本文汇总了Smarty中常用变量操作符,分享给大家供大家参考.具体如下: php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符. 语法中使用"|"应用变量操作符,多个参数用":"??指簟?/DIV> capitalize[首字母大写] count_characters[计算字符数] cat[连接字符串] count_paragraphs[计算段落数] count_sentences[计算句数] count_words[计算词数] date_f

  • smarty半小时快速上手入门教程

    本文讲述了smarty快速上手入门的方法,可以让读者在半小时内快速掌握smarty的用法.分享给大家供大家参考.具体实现方法如下: 一.smarty的程序设计部分: 在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计.下载Smarty文件放到你们站点中. index.php代码如下: 复制代码 代码如下: <?php /** * * @version $Id: index.php * @package

  • php smarty truncate UTF8乱码问题解决办法

    估计不少玩smarty模板的小朋友都遇到过裁切乱码问题.特别是UTF8编码的. 以下代码保存为modifier.truncate2.php 存到smarty libs下的plugin目录下 然后裁剪的时候用$v->content|truncate2:100 就搞定了. 如果不好用可能是缓存导致,请速度删除templates_c下的缓存文件(小编搞的时候遇到缓存问题.) 复制代码 代码如下: <?php/** * Smarty plugin * @package Smarty * @subpac

  • smarty中先strip_tags过滤html标签后truncate截取文章运用

    strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 复制代码 代码如下: <?php echo strip_tags("Hello <b>world!</b>"); ?> smarty中可以使用strip_tags去除html标签,包括在< >之间的任何内容. 例如: index.php: 复制代码 代码如下: $smarty = new Smarty; $smarty->assign('articleTi

  • smarty中改进truncate使其支持中文的方法

    本文实例讲述了smarty中改进truncate使其支持中文的方法.分享给大家供大家参考,具体如下: smarty的truncate不支持中文的截取.将smarty目录下plugins中的modifier.truncate.php改成下面这个样子就可以了 <?php /* * Smarty plugin * ------------------------------------------------------------- * Type: modifier * Name: truncate

  • smarty模板中拼接字符串的方法

    PHP页面传到Smarty模板上的变量(这里用,在Smarty上创建两个变量代替) 复制代码 代码如下: {assign var="name" value='Richard.Lee'}{assign var="age" value='27'} 1.想要在Smarty模板的某个位置输出(Richard.Lee---27)拼接方法:{$name|cat:"---"|cat:$age}解释:将变量$name."---".$age 拼

  • php smarty函数扩展

    中文截取 modifier.cn_truncate.php 复制代码 代码如下: 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 =

  • Zend Framework框架Smarty扩展实现方法

    本文实例讲述了Zend Framework框架Smarty扩展实现方法.分享给大家供大家参考,具体如下: 今天总结一下ZF框架中扩展Smarty模板的方法,在ZF帮助文档中已经有比较详细的介绍,在这我稍微多说一些. 一.将smarty的核心文件包放在lib文件夹下,文件包中要包括(internals/,plugins/,Config_File.class.php,Smarty.class.php,Smarty_Compiler.class.php,debug.tpl). 二.在Zend/View

  • 解析smarty 截取字符串函数 truncate的用法介绍

    smarty truncate 截取字符串从字符串开始处截取某长度的字符,默认的长度为80指定第二个参数作为截取字符串的长度默认情况下,smarty会截取到一个词的末尾,如果需要精确到截取多少个字符可以使用第三个参数,将其设为"true"具体用法如下: 复制代码 代码如下: //index.php $smarty = new Smarty; $smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Ye

  • SQL Server中常用截取字符串函数介绍

    SQL Server中一共提供了三个字符串截取函数:LEFT().RIGHT().SUBSTRING(). 一.LEFT()函数 函数说明如下: 语法:LEFT(character,integer). 参数介绍:参数1:要截取的字符串,参数2:截取字符个数. 返回值:返回从字符串左边开始指定个数的字符. 示例SQL:select LEFT('SQLServer_2012',3). 返回:SQL. 二.RIGHT()函数 函数说明如下: 语法:RIGHT(character,integer). 参

  • Go语言截取字符串函数用法

    本文实例讲述了Go语言截取字符串函数用法.分享给大家供大家参考.具体如下: 复制代码 代码如下: func Substr(str string, start, length int) string {     rs := []rune(str)     rl := len(rs)     end := 0             if start < 0 {         start = rl - 1 + start     }     end = start + length        

  • ThinkPHP的截取字符串函数无法显示省略号的解决方法

    对于ThinkPHP的截取字符串函数无法显示省略号的情况,解决方法如下: 打开Common/extend.php页面,修改msubstr函数如下: function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_exists("mb_substr")) { if($suffix) { if($str==mb_substr($str, $start, $le

  • Python正则替换字符串函数re.sub用法示例

    本文实例讲述了Python正则替换字符串函数re.sub用法.分享给大家供大家参考,具体如下: python re.sub属于python正则的标准库,主要是的功能是用正则匹配要替换的字符串 然后把它替换成自己想要的字符串的方法 re.sub 函数进行以正则表达式为基础的替换工作 下面是一段示例源码 #!/usr/bin/env python #encoding: utf-8 import re url = 'https://113.215.20.136:9011/113.215.6.77/c3

  • ThinkPHP 模板substr的截取字符串函数详解

    ThinkPHP 模板substr的截取字符串函数 在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){ if(function_exists("mb_substr")){ $slice= mb_substr($str, $start, $length, $charset); }e

  • smarty内置函数section的用法

    本文实例讲述了smarty内置函数section的用法.分享给大家供大家参考.具体分析如下: foreach函数可以做到section能做的一切,所以一般都用foreach,这里也详细说下section的用法.section只能遍历索引数组,而不能遍历关联数组. 数组键值按倒序输出实例: 模板文件:temp.htm 复制代码 代码如下: {section name=foo loop=$name step=-1} {$name[foo]} {/section} 参数说明: name为section

  • MySQL 截取字符串函数的sql语句

    1.left(name,4)截取左边的4个字符 列: SELECT LEFT(201809,4) 年 结果:2018 2.right(name,2)截取右边的2个字符 SELECT RIGHT(201809,2) 月份 结果:09 3.SUBSTRING(name,5,3) 截取name这个字段 从第五个字符开始 只截取之后的3个字符 SELECT SUBSTRING('成都融资事业部',5,3) 结果:事业部 4.SUBSTRING(name,3) 截取name这个字段 从第三个字符开始,之后

  • Thinkphp模板中截取字符串函数简介

    在php中截取字符串的函数有很多,而在thinkphp中也可以直接使用php的函数,本文给大家简单的介绍thinkPHP模板中截取字符串的具体用法,希望能对各位有所帮助. 对于英文字符可使用如下形式: 复制代码 代码如下: {$vo.title|substr=0,5} 如果是中文字符thinkphp提供了msubstr,用法如下: 复制代码 代码如下: function msubstr($str, $start=0, $length, $charset="utf-8″, $suffix=true

  • ASP中Split分割字符串函数的实例用法

    ASP中Split函数的用法 分割截取字符串看几个例子就能理解了 复制代码 代码如下: mystr="1,2,3,4,5" mystr=split(mystr,",") for i=0 to ubound(mystr) response.write mystr(i) next  '返回值为123456 mystr="xlei.net/http/student/x/index.asp" mystr=split(mystr,"/http/s

随机推荐