The substr_replace() function is used to replace a portion of a string with another string. It is a commonly used function in PHP for string manipulation, allowing for flexible replacement of specific parts of the target string.
substr_replace(str, replacement, begin, len)
The substr_replace() function returns the string after replacement.
The following code demonstrates how to replace a part of a string starting from a specified position:
<?php
echo substr_replace("Demo text", "word", 5);
?>
Demo word
This example shows how to replace a portion of the string starting from the end:
<?php
echo substr_replace("Demo text", "word", -5);
?>
Demoword
From the above examples, we can see that the PHP substr_replace() function is versatile and allows for flexible string replacement, whether starting from the beginning, middle, or end of the string. It is an essential function for efficient string manipulation in PHP.