{str_replace:search:replace:text}

Description

Replaces text, the AA counterpart of the PHP str_replace function. The arguments are, in order, what to search for, what to replace it with, and the text to search in. With plain strings it swaps one term for another; pass a JSON array of strings as the search to replace several terms at once, and a JSON array as the replacement to map each search term to its own replacement by position. Replacing with an empty value deletes the match. Matching is case-sensitive, and the arguments are not trimmed, so spaces in the search and replacement are significant. For pattern-based replacement use replace instead.

Parameters

search required default (empty)

The text to find. A plain string finds one term; a JSON array of strings finds several terms at once. Not trimmed, so leading/trailing spaces are significant.

replace optional default (empty - deletes the match)

What to put in place of each match. A plain string replaces every search term with the same value; a JSON array maps each search term to the replacement at the same position. An empty value deletes the match. Not trimmed.

text required default (empty)

The string to search in. The result is text with every match replaced.

Examples

test{str_replace:uno:one:text with uno inside}
Expectedtext with one inside
Actualtext with one inside
Replaces every occurrence of the search string. Search and replace are plain text here.
test{str_replace:Cat:dog:cat Cat CAT}
Expectedcat dog CAT
Actualcat dog CAT
Matching is case-sensitive: only Cat changes, not cat or CAT.
test{str_replace:["cat","dog"]:animal:a cat and a dog}
Expecteda animal and a animal
Actuala animal and a animal
Search is a JSON array, replace is a single string: every listed term is replaced by the same value.
test{str_replace:["red","blue"]:["green","yellow"]:red sky, blue sea}
Expectedgreen sky, yellow sea
Actualgreen sky, yellow sea
Both arguments are JSON arrays of equal length: each search term maps to the replacement at the same position.
test{str_replace:["-"]:[""]:2024-03-15}
Expected20240315
Actual20240315
Replacing with an empty value deletes the search string. Here all dashes are stripped from a date.