How to insert a string into another string in PHP?
1. Code snippets
In this post, we help you how to insert a string into another string from specified position in PHP.
<?php
$str = "is providing PHP tutorials, tips, tricks and code snippets";
$subStr = "Soltuts.com ";
$st = addSubStr2Pos($str, $subStr, 0);
echo($st);
function addSubStr2Pos($str, $subStr, $position){
if (!isset($str) || trim($str) === '') {
return "";
}
return substr_replace($str, $subStr, $position, 0);
}
?>
Output
Soltuts.com is providing PHP tutorials, tips, tricks and code snippets
2. Example
Input
originalString = "is providing PHP tutorials, tips, tricks and code snippets" stringToBeInserted = "Soltuts.com " index = 0
Output
Soltuts.com is providing PHP tutorials, tips, tricks and code snippets
3. Download this Example from Github
Have a nice day!
SolTuts 🙂