Home > Article > Backend Development > substr_replace() function in PHP
The substr_replace() function is used to replace part of a string with another string.
substr_replace(str,replacement,begin,len)
str - The string to check
replacement - The string to be inserted
begin - The position where the replacement begins -
If begin is a positive number: start replacing from the specified position in the string
If begin is n number: - start replacing at the specified position from the end of the string
If begin is 0 - start replacing from the first character in the string.
len - The number of characters to replace. Defaults to the same length as the string.
If len is a positive number - the length of the string to be replaced
If len is a negative number - how much should be left at the end of the string after replacement characters
If len is 0 - insert instead of replace.
substr_replace() function returns the replaced string.
The following is an example-
Live demonstration
<?php echo substr_replace("Demo text","word",5); ?>
The following is the output-
Demo word
Let's see another example, replace string from end of string -
Live Demonstration
<?php echo substr_replace("Demo text","word",-5); ?>
Here is the output-
Demoword
The above is the detailed content of substr_replace() function in PHP. For more information, please follow other related articles on the PHP Chinese website!