replace()
Definition and usage
Replaces literal text or a delimited regular-expression pattern.
Syntax
replace(pattern, replacement, subject)
Parameter values
| Parameter | Allowed type | Required | Description |
pattern |
string |
Yes |
Literal text or a delimited regex. |
replacement |
string |
Yes |
Replacement text. |
subject |
mixed |
Yes |
Source value converted to text. |
Return value
| Return type | string |
| Output | Updated text; the original text is returned if a regex is invalid. |
Examples
Example 1
replace(" ", "-", "admin portal")
Expected return value: admin-portal
Example 2
replace("/[0-9]+/", "N", "port123")
Expected return value: portN
Behavior:
Regex replacement uses PHP-compatible delimiters, but only inside this function; matches_regex is used for boolean regex tests.