Home >>PHP String Functions >PHP substr_replace() Function
PHP substr_replace() function is used to replace a part of a given string with another string. The start position and the length of the string is passed as a parameter and if the start is negative and length is less than or equal to start then length become 0 (zero). It is binary-safe.
Syntax:
substr_replace($string, $replace, $start, $length);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter defines the string to check. |
replace | This is a required parameter. This parameter defines the string to insert. |
start | This is a required parameter. This parameter defines where to start replacing in the string. |
length | This is an optional parameter. This parameter defines how many characters should be replaced. |
Here is an example of substr_replace() function in PHP:
<html> <body> <?php $str1 = "Python"; $str2 = "PHP"; echo substr_replace($str1,$str2,0); ?> </body> </html>
Here is an example of substr_replace() function in PHP:
<html> <body> <?php $str1 = "Hello !! I am Jerry.."; $str2 = "Everyone"; echo substr_replace($str1,$str2,6,0); // it will insert str2 at position 6 ?> </body> </html>