Skip to content

Core\Str::replace

Core\Str::replace(
    string $s,
    string $search,
    string $replacement,
    {caseInsensitive?: bool, limit?: uint},
): string

Replaces a piece of text everywhere it appears in a string.

Core\Str::replace takes a string, the text to find and the text to put in its place. It returns a new string: replace("a cat and a cat", "cat", "dog") gives "a dog and a dog". If the text is not found, the string is returned unchanged. An empty search text matches nothing.

Matches are found from left to right and do not overlap. The new text is not searched again, so replace("aaa", "aa", "a") gives "aa".

Two options change the search. caseInsensitive: true lets upper-case and lower-case letters match each other, so "Draft" also finds "DRAFT". limit sets the most matches to replace, counted from the left. limit: 0 replaces nothing.

To replace several different texts in one call, use Core\Str::replaceAll.

Parameters

  • $sstringThe string to change.
  • $replacementstringThe text that replaces each match.

Options

  • caseInsensitivebooloptionalIf true, upper-case and lower-case letters match each other, so "A" finds "a". The default is false.
  • limituintoptionalThe most matches to replace, counted from the left. 0 replaces nothing. The default is every match.

Return value

Returns string. The changed string. If nothing matched, the result is $s unchanged.

Examples

Change a word

<?nvs
// Shows how to replace a word in a sentence.
string $text = 'The cat sat on the mat. The cat slept.';
// Every "cat" is replaced with "dog".
echo Core\Str::replace($text, 'cat', 'dog'), "\n";
// `limit: 1` replaces only the first match.
echo Core\Str::replace($text, 'cat', 'dog', { limit: 1 }), "\n";
// The text has no "bird", so the result is the text unchanged.
echo Core\Str::replace($text, 'bird', 'dog'), "\n";
Output
The dog sat on the mat. The dog slept.
The dog sat on the mat. The cat slept.
The cat sat on the mat. The cat slept.

Ignore upper and lower case

<?nvs
// Shows how to replace a word however it is written: "draft", "Draft" or "DRAFT".
string $title = 'DRAFT: Price list (Draft 2, draft 3)';
// Only the exact text "draft" matches, so one word changes.
echo Core\Str::replace($title, 'draft', 'final'), "\n";
// With `caseInsensitive: true`, all three words match.
echo Core\Str::replace($title, 'draft', 'final', { caseInsensitive: true }), "\n";
Output
DRAFT: Price list (Draft 2, final 3)
final: Price list (final 2, final 3)

One line per log entry

<?nvs
// Shows how to keep a log entry on one line when a message contains line breaks.
string $message = "Payment failed.\r\nCard declined.\nPlease try again.";
// Windows ends a line with `\r\n`, and other systems with `\n`.
// The first call replaces each `\r\n`. The second replaces each `\n` that is left.
string $line = Core\Str::replace($message, "\r\n", ' ');
$line = Core\Str::replace($line, "\n", ' ');
echo '[error] ', $line, "\n";
Output
[error] Payment failed. Card declined. Please try again.