This will do the job. It uses substr_replace() . You can learn more about this here .
<?php $stringToInsert = "tesssst"; $oldString = "<!-- BEGIN INSERT 1 --><!-- END INSERT 1 -->"; $newString = substr_replace($oldString, "-->" . $stringToInsert . "<!--", strpos($oldString, "--><!--"), strlen($stringToInsert));
source share