Last Updated: July 06, 2022
·
67.9K
· dfeddad

Simple way to insert a string into another string at any position in PHP

If you want a very simple and short solution to insert string into another string use:
substr_replace ( $string , $replacement , $start , 0 ). The key here is zero, and because we set $length to zero this function will have the effect of inserting the replacement into the string.

<?php

$string = 'I am happy today.';
$replacement = 'very ';

echo substr_replace($string, $replacement, 4, 0); // I am very happy today.