PHP » Strings » substr_replace()

Syntax:
string substr_replace(string s, string replace, int start [, int length])
s
The string.
replace
Replacement string.
start
Start position.
length
Number of characters to replace.

Replaces part of a string.

This function replaces a portion of a string with another string, starting at the specified position. A negative start position gives an offset from the end of the string. By default, the rest of the string after the starting position is replaced with the replacement string. If a length is specified, that will be the number of characters replaced. If length is negative it specifies a number of characters counted from the end of the string that should be spared from replacement.

Examples

Code:
<?php

print substr_replace("0123456789", "111", 1) . "<br>";
print substr_replace("0123456789", "555", -4) . "<br>";
print substr_replace("0123456789", "111", 1, 2) . "<br>";
print substr_replace("0123456789", "777", -3, -1) . "<br>";

?>
Output:
0111
012345555
01113456789
01234567779
Explanation:

This code demonstrates the effects of different arguments to substr_replace().

See Also: