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

yipeiwu_com5年前PHP代码库
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…

相关文章

定义php常量的详解

常量可以理解为值不变的变量。常量值被定义后,在脚本的其他任何地方都不能被改变。一个常量由英文字母、下划线、和数字组成,但  数字不能作为首字母出现。在php中使用defaine...

PHP遍历某个目录下的所有文件和子文件夹的实现代码

复制代码 代码如下:<?php function read_all_dir ( $dir )    {   &...

php实现斐波那契数列的简单写法

斐波那契数列是非常常见的一类数列,其数学定义为:F0=1,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)。本文就用php来简单实现斐波那契数列,代码十分简洁易懂,...

php curl获取网页内容(IPV6下超时)的解决办法

原因:在程序中我对curl获取内容都作了较为严格的超时限制,所以就会造成无法获取内容的问题。解决方法:设置默认访问为ipv4。php的curl设置方法如下:复制代码 代码如下:<?...

PHP base64编码后解码乱码的解决办法

在用PHP做东西的时候发现了一个问题,可以简单的归结为乱码的问题,但是这个问题不是函数本身造成的。来看看罪魁祸首是谁。 嫌疑人:base64_encode 和 base64_decode...