regex_replace

Parameter PositionTypeRequiredDefaultDescription
1stringYesn/aThis is the regular expression to be replaced.
2stringYesn/aThis is the string of text to replace with.

A regular expression search and replace on a variable. Use the syntax for preg_replace() from the PHP manual.

正規表示式會在變數中作搜尋與取代的動作,使用的文法是 PHP 的 preg_replace()。

Example 5-14. regex_replace

<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely to\nbe passed on, experts say.");
$smarty->display('index.tpl');

?>

Where index.tpl is:

其中 index.tpl 的內容為:

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

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

This should output:

輸出結果為:

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