Skip to content

Core\Str::replaceRange

Replaces a part of a string, chosen by its position, with a new text.

Core\Str::replaceRange takes a string, an offset, a length and a replacement. The offset and the length choose the part to replace, in the same way as Core\Str::slice: the offset is where the part starts, and the length is how many characters it has. A negative offset counts from the end. A negative length stops that many characters before the end, and null means "to the end".

An empty replacement deletes the part. A length of 0 deletes nothing, so the replacement is inserted at the offset. Positions count characters as a person sees them, not bytes, so "é" is one character.

To replace a text wherever it appears, use Core\Str::replace.

Parameters

  • $sstringThe string to change.
  • $offsetintThe character where the part starts. 0 is the first character. A negative offset counts from the end.
  • $length?intHow many characters the part has. A negative length stops that many characters before the end. null means the part runs to the end.
  • $replacementstringThe text that replaces the part. "" deletes the part.

Return value

Returns string. The changed string. If the part is empty, for example with a $length of 0, $replacement is inserted at $offset.

Examples

Change one word

<?nvs
// Shows how to replace a part of a string when you know where it starts and how long it is.
string $greeting = 'Good morning, Anna!';
// The part starts at character 5 and has 7 characters: "morning".
// This prints "Good evening, Anna!".
echo Core\Str::replaceRange($greeting, 5, 7, 'evening'), "\n";
// A negative offset counts from the end. The part "Anna!" starts 5 characters before the end.
// `null` as the length means the part runs to the end. This prints "Good morning, Ben!".
echo Core\Str::replaceRange($greeting, -5, null, 'Ben!'), "\n";
Output
Good evening, Anna!
Good morning, Ben!

Insert and delete

<?nvs
// Shows how to insert a text and how to delete a part of a string.
string $date = '20260928';
// A length of 0 replaces nothing, so the text is inserted at the offset.
// This prints "2026-09-28".
string $withDashes = Core\Str::replaceRange(Core\Str::replaceRange($date, 6, 0, '-'), 4, 0, '-');
echo $withDashes, "\n";
// An empty replacement deletes the part. This prints "2026-09".
echo Core\Str::replaceRange($withDashes, 7, null, ''), "\n";
// Positions count characters, not bytes. "é" is one character, so this prints "Café".
echo Core\Str::replaceRange('Cafe', 3, 1, 'é'), "\n";
Output
2026-09-28
2026-09
Café

Hide a card number

<?nvs
// Shows how to hide a card number on a receipt, so only the last four digits are visible.
array<string> $cards = ['4111111111111111', '5500005555555559', '340000000000009'];
foreach ($cards as string $card) {
// A length of -4 stops 4 characters before the end.
// Everything before the last four digits is replaced with the same number of "*".
uint $hidden = Core\Str::length($card) - 4;
echo Core\Str::replaceRange($card, 0, -4, Core\Str::repeat('*', $hidden)), "\n";
}
Output
************1111
************5559
***********0009