网站制作学习网PHP→正文:smarty判断字符串内含有字符
字体:

smarty判断字符串内含有字符

PHP 2009/11/12 9:07:46  点击:不统计

在用smarty做php中遇到了怎么在smarty中替换字符串,也就是说 smarty中如何判断字符串包含另一个字符串其实在smarty的说明文档中有两个这样的教程.一个是直接替换一个是正则表达式替换.
直www.接forasp替.cn换的语法如下(在这里使用<{}>定义模板起止符):
<{$oldstring|replace:" 要替换的字符串":"替换成为的字符串"}>
实际例子:页面代码
$smarty = new Smarty;
$smarty->assign('oldstring', "这里是你原来定义的字符串");
$smarty->display('index.tpl');
模板代码
index.tpl:
模板内代码
{$oldstring}
{$oldstring|replace:"原来":"现在"}
{$oldstring|replace:"原来":""}
运行输出为:
这里是你原来定义的字符串
这里是你现在定义的字符串
这里是你定义的字符串
www.forasp.cn 原创,参考smarty html介绍,转载请注明
下面再是 正则表达式的语法,也称为 正则替换
语法:{$oldstring|regex_replace:"正则表达式":"要替换的内容"}
实际例子:
$smarty = new Smarty;
$smarty->assign('oldstring', "这里是你原来定义的字符串\n后显示的内容");
$smarty->display('index.tpl');
模板代码
index.tpl:
模板内代码
{$oldstring}
{$oldstring|regex_replace:"/[/n]/":"换行"}
运行输出为:
这里是你原来定义的字符串
换行后显示的内容
www.forasp.cn 网站制作学习网
这里是你定义的字符串换行后显示的内容

网站制作学习网(http://www.forasp.cn)原创,转载请注明!

·上一篇:PHP毫秒时间差 >>    ·下一篇:PHP下载图片 >>
推荐文章
最新文章