Easy Tutorial
❮ Func Ftp Get Func Ftp Exec ❯

PHP substr_replace() Function

PHP String Reference Manual

Example

Replace "Hello" with "world":


Definition and Usage

The substr_replace() function replaces a part of a string with another string.

Note: If the start parameter is negative and the length is less than or equal to start, the length is 0.

Note: This function is binary-safe.


Syntax

Parameter Description
string Required. Specifies the string to check.
replacement Required. Specifies the string to insert.
start Required. Specifies where in the string to start replacement. Positive - At the specified position in the string<br> Negative - At the specified position from the end of the string<br> 0 - At the first character in the string
length Optional. Specifies how many characters to replace. Default is the same length as the string. Positive - The length of the replaced string<br> Negative - The number of characters from the end of the string to be replaced<br> 0 - Insert instead of replace

Technical Details

Return Value: Returns the replaced string. If string is an array, it returns an array.
PHP Version: 4+
--- ---
Changelog: As of PHP 4.3.3, all parameters accept arrays.
--- ---

More Examples

Example 1

Replace starting from the 6th position in the string (replace "world" with "earth"):

Example 2

Replace starting from the 5th position from the end of the string (replace "world" with "earth"):

Example 3

Insert "Hello" at the beginning of "world":

Example 4

Replace "AAA" with "BBB" in multiple strings at once:


❮ Func Ftp Get Func Ftp Exec ❯