Core\Str::replaceAll
Replaces several different texts in a string with one call.
Core\Str::replaceAll takes a string and an array of pairs. Each key of the array is a text to find, and its value is the text that replaces it: replaceAll("{name} is here", ["{name}" => "Anna"]) gives "Anna is here". If nothing matches, the string is returned unchanged.
The string is read once, from left to right. The new text is not searched again, so ["left" => "right", "right" => "left"] makes the two words change places. Where several keys match at the same place, the longest key is used. The order of the pairs does not change the result. An empty key is ignored.
A key matches only where it does not split a character, the way a person reads it. "\r\n" is one line ending, and it counts as one character. So the key "\r" does not match inside it, and ["\r" => ""] leaves Windows line ends unchanged. To change them, add "\r\n" as a key of its own: ["\r\n" => "\n", "\r" => ""].
caseInsensitive: true lets upper-case and lower-case letters match each other.
To replace one text, use Core\Str::replace.
Parameters
$sstringThe string to change.$pairsarray<string>The replacements. Each key is the text to find, and its value is the text that replaces it. An empty key is ignored. A key matches only where it does not split a character, so"\r"does not match inside"\r\n".
Options
caseInsensitivebooloptionalIftrue, upper-case and lower-case letters match each other. If two keys then match the same text, the key written first is used. The default isfalse.
Return value
Returns string. The changed string. If $pairs is empty or nothing matched, the result is $s unchanged.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Str::startsWith, Core\Str::endsWith and Core\Str::replaceAll find a match only where it does not split a character. So Core\Str::startsWith("e\u{301}", "e") is false, because the accent belongs to the e. The pair "\r\n" is one character, so "\r" alone does not match inside it. |
Examples
Fill in a template
<?nvs// Shows how to fill in the placeholders of a message template.
string $template = 'Hello {name}, your order {order} has shipped.';
// Each key is a placeholder, and its value is the text that replaces it.array<string> $values = ['{name}' => 'Anna', '{order}' => '#1042'];
// This prints "Hello Anna, your order #1042 has shipped."echo Core\Str::replaceAll($template, $values), "\n";
// `{title}` is not a key in the array, so it stays as it is.echo Core\Str::replaceAll('Dear {title} {name},', $values), "\n";Hello Anna, your order #1042 has shipped.Dear {title} Anna,Read once from left to right
<?nvs// Shows how `Core\Str::replaceAll` chooses what to replace.
// "left" becomes "right", and "right" becomes "left".// The new text is not searched again, so the two words change places.echo Core\Str::replaceAll('turn left, then right', ['left' => 'right', 'right' => 'left']), "\n";
// "New" and "New York" both match at the start. The longest key is used,// so the result is "NYC".echo Core\Str::replaceAll('New York', ['New' => 'Old', 'New York' => 'NYC']), "\n";
// With `caseInsensitive: true`, "YES" and "Yes" also match the key "yes".echo Core\Str::replaceAll('yes, YES, Yes', ['yes' => 'no'], { caseInsensitive: true }), "\n";turn right, then leftNYCno, no, noA safe file name
<?nvs// Shows how to turn a document title into a file name that works on every system.
string $title = 'Größe & Maße: Übersicht 2026';
// Each German letter becomes plain letters, and "&", ":" and spaces become "-".// The key " & " is longer than the key " ", so " & " is used where both match.array<string> $safe = [ 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ß' => 'ss', ' & ' => '-', ': ' => '-', ' ' => '-',];
// This prints "Groesse-Masse-Uebersicht-2026.pdf".echo Core\Str::replaceAll($title, $safe), ".pdf\n";Groesse-Masse-Uebersicht-2026.pdf