codecamp

Smarty:regex_replace

regex_replace

用正则表达式搜索和替换变量值。 使用PHP的 preg_replace()函数进行。

温馨提示:

虽然Smarty提供了较方便的正则表达式修饰器, 但通常更好的方式是通过自定义函数或自定义修饰器在PHP端进行正则匹配替换。 正则表达式是程序应用代码,不是显示的逻辑代码。

Parameters

参数顺序类型必选参数默认值说明
1stringYesn/a正则表达式
2stringYesn/a替换的字符

Example 5.15. regex_replace

<?php

$smarty->assign('articleTitle', "Infertility unlikely to\nbe passed on, experts say.");

?>

模板:

{* replace each carriage return, tab and new line with a space *}

{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}

输出:

Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.

参见 replace 和 escape.

Smarty:nl2br
Smarty:replace
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

I.Smarty基础

1.Smart是什么?

II.Smarty模板设计师篇

6.Smarty复合修饰器

9.Smarty配置文件

10.Smarty调试控制台

III. 程序开发者篇

11. Smarty字符集编码

12.Smarty常量

13.Smarty成员变量

14.Smarty成员方法

17.Smarty高级特性

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }