Skip to content

Core\Regex::replaceWith

Finds every match of a pattern in a text and replaces it with a text that your own function returns. It returns the new text. If there is no match, it returns the same text and the function is not called.

The function gets one Core\Regex\Match for each match, from left to right. It can read the whole match and every group, and it returns a string. That text is inserted exactly as it is, so $1 in it is two characters. The limit option replaces only the first matches.

Good to know: if the replacement is the same text for every match, Core\Regex::replace is shorter.

The examples show a simple function and limit, reading a group of each match, and filling the placeholders in an email text.

Parameters

  • $subjectstringThe text to search.
  • $patternCore\Regex\Pattern|stringA Core\Regex\Pattern from Core\Regex::compile, or a pattern string with no options. A tainted string does not compile here.
  • $fncallable(Core\Regex\Match): stringA function that gets one Core\Regex\Match and returns a string. It is called once for each match, from left to right. The text it returns is inserted as it is, so $1 in it is two characters.

Options

  • limituintoptionalHow many matches to replace, counted from the start of the text. The default is every match. 0 replaces nothing. The function is not called for a match after the limit.

Return value

Returns string. The text with the matches replaced. It is the same text if there is no match.

Errors

  • RuntimeError$pattern is not a valid regular expression, matching it against $subject needs more steps than the limit allows, or the new text is larger than the memory limit.

Examples

Make each match upper case

<?nvs
// Shows how to replace each match with a text that your own function builds.
string $text = 'the red fox and the blue bird';
// The function gets each match and returns it in upper case.
// This prints "the RED fox and the BLUE bird".
echo Core\Regex::replaceWith($text, '\b(red|blue)\b', fn(Core\Regex\Match $m) => Core\Str::upper($m->text())), "\n";
// `limit: 1` replaces only the first match. This prints "the RED fox and the blue bird".
echo Core\Regex::replaceWith($text, '\b(red|blue)\b', fn(Core\Regex\Match $m) => Core\Str::upper($m->text()), { limit: 1 }), "\n";
// There is no match here, so the function is not called and the text does not change.
// This prints "the red fox and the blue bird".
echo Core\Regex::replaceWith($text, '[0-9]+', fn(Core\Regex\Match $m) => '#'), "\n";
Output
the RED fox and the BLUE bird
the RED fox and the blue bird
the red fox and the blue bird

Use a group of each match

<?nvs
// Shows how to read a group of each match inside the function.
string $prices = 'Price: 12 USD, shipping: 3 USD';
// `(\d+) USD` matches an amount, and group 1 is the number. The function puts
// a `$` sign before the number. This prints "Price: $12, shipping: $3".
echo Core\Regex::replaceWith($prices, '(\d+) USD', fn(Core\Regex\Match $m) => '$' . ($m->group(1) ?? '')), "\n";
// The text that the function returns is inserted exactly as it is.
// `$1` is not a group here, so this prints "Price: $1, shipping: $1".
echo Core\Regex::replaceWith($prices, '(\d+) USD', fn(Core\Regex\Match $m) => '$1'), "\n";
Output
Price: $12, shipping: $3
Price: $1, shipping: $1

Fill the placeholders in an email

<?nvs
// Shows how to fill the placeholders in an email text with values from an array.
array<string> $values = ['name' => 'Ana', 'order' => '1042'];
string $email = 'Hello {name}, your order {order} has shipped. {coupon}';
// `\{(\w+)\}` matches a word in curly brackets, and group 1 is the word. The function
// returns the value for that word. A word with no value keeps its placeholder.
string $filled = Core\Regex::replaceWith($email, '\{(\w+)\}', fn(Core\Regex\Match $m) => $values[$m->group(1) ?? ''] ?? $m->text());
// This prints "Hello Ana, your order 1042 has shipped. {coupon}".
echo $filled, "\n";
Output
Hello Ana, your order 1042 has shipped. {coupon}