跳至内容

regex_replace

变量上的正则表达式搜索和替换。使用 PHP 手册中的 preg_replace() 语法。

基本用法

{$myVar|regex_replace:"/foo/":"bar"}

注意

虽然 Smarty 提供了此 regex 便捷修改符,但通常最好通过自定义函数或修改符在 PHP 中应用正则表达式。正则表达式被认为是应用程序代码,不属于演示逻辑。

参数

参数位置 类型 必需 描述
1 字符串 要替换的正则表达式。
2 字符串 要替换成的文本字符串。

示例

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

另请参阅 replaceescape