PHP substr_replace() Function


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

Syntax
substr_replace(string,replacement,start,length)
Parameters
  • String - (Required) Specify the string to check.
  • replacement - (Required) Specify the string to insert.
  • start - (Required) Specify where to start replacing in string.
    A Given number is positive number ,Start replacing at the specified position in the string
    A Given number is negative number ,Start replacing at the specified position from the end of the string
    0 - Start replacing the first character in string.

  • length - (Required) Specify the how many characters should be replaced.
    A Given number is positive number ,length of string to be replaced
    A Given number is negative number ,How many characters should be left at end of string after replacing
    0 - Insert instead of replace.
Example
<?php
    echo substr_replace("Good Morning. How was a day ?,They day is Good","Good");
?>

Output

2
Example
<?php
    $a = "This day is very nice";
    echo strlen($a )."<br>";
    echo substr_replace($a ,"day")."<br>"; 
    echo substr_replace($a,"day",2)."<br>";
    echo substr_replace($a,"is",3,3)."<br>";
?>

Output

21
1
1
0

Prev Next