PHP Program to add string between another string using substr_replace() function

How to add the string in between another string using PHP?

In this program, we are adding the string into the previously entered string. To do so we are using the built-in function substr_replace(). For this, we first have to assign the string into the variable string then assign the string which should be added to the original string into the variable w and assign the position where the string should be added to the variable pos. After that to add the variable w into the variable string use the built-in function substr_replace(string, word, pos, 0) and at last print the value of the variable string

Syntax of function substr_replace()


substr_replace(string,replacement,start,length)
 

ALGORITHM

Step 1: Assign the string into the variable string

Step 2: Assign the word which should be added to the string into the variable w

Step 3: Assign the position where the string should be added to the variable pos

Step 4: Add the value in the variable w to the variable string by using the built-in function substr_replace(string, word, pos,0) and print the result

PHP Source Code

                                          <?php
$string = 'learntutorials.com';
echo "Orginal string: $string \n";
$w = 'E';
$pos = 5;
echo "String after change: " . substr_replace($string, $w, $pos, 0);
?>
                                      

OUTPUT

Orginal string: learntutorials.com
String after change: learnEtutorials.com