Files
smarty/docs/designers/language-modifiers/language-modifier-replace.md

46 lines
1.1 KiB
Markdown
Raw Permalink Normal View History

2023-02-06 14:42:31 +01:00
# replace
A simple search and replace on a variable. This is equivalent to the
2023-02-06 14:42:31 +01:00
PHP's [`str_replace()`](https://www.php.net/str_replace) function.
2023-02-06 14:42:31 +01:00
## Basic usage
```smarty
{$myVar|replace:"foo":"bar"}
```
2023-02-06 14:42:31 +01:00
## Parameters
2023-02-06 14:42:31 +01:00
| Parameter Position | Type | Required | Description |
|--------------------|--------|----------|---------------------------------------------|
| 1 | string | Yes | This is the string of text to be replaced. |
| 2 | string | Yes | This is the string of text to replace with. |
2023-02-06 14:42:31 +01:00
## Examples
2023-02-06 14:42:31 +01:00
```php
<?php
2023-02-06 14:42:31 +01:00
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
2023-02-06 14:42:31 +01:00
```
2023-02-06 14:42:31 +01:00
Where template is:
2023-02-06 14:42:31 +01:00
```smarty
{$articleTitle}
{$articleTitle|replace:'Garden':'Vineyard'}
{$articleTitle|replace:' ':' '}
```
Will output:
2023-02-06 14:42:31 +01:00
```
Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
```
2023-02-06 14:42:31 +01:00
See also [`regex_replace`](language-modifier-regex-replace.md) and
[`escape`](language-modifier-escape.md).