Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP substr_replace() Function

PHP substr_replace() Function

PHP String Reference Manual PHP String Reference Manual

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 - Start at the specified position in the string
  • Negative - Start at the specified position from the end of the string
  • 0 - Start at the first character of the string
  • | | *length* | Optional. Specifies how many characters to replace. Default is the same as the string length. |

    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 PHP String Reference Manual