PHP substr_replace() Function
Example
Replace "Hello" with "world":
Run Example »
Definition and Usage
The substr_replace() function replaces part of a string with another string.
**Note:** If the start parameter is negative and length is less than or equal to start, then length is 0.
**Note:** This function is binary-safe.
Syntax
substr_replace(
string,replacement,start,length
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| replacement | Required. Specifies the string to insert. |
| start | Required. Specifies where in the string to start the replacement.
|
- Positive - Length of the replaced string
- Negative - Number of replaced characters from the end of the string
- 0 - Insert instead of replace
Technical Details
| Return Value: | Returns the replaced string. If string is an array, an array is returned. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | Since PHP 4.3.3, all parameters accept arrays. |
More examples
Example 1
Start the replacement from position 6 of the string (replace "world" with "earth"):
Run Example »
Example 2
Start the replacement from the 5th position from the end of the string (replace "world" with "earth"):
Run Example »
Example 3
Insert "Hello" before "world":
Run Example »
Example 4
Replace multiple strings at once. Replace "AAA" in each string with "BBB":
",substr_replace($replace,'BBB',3,3)); ?>Run Example »
PHP String Reference Manual