substr_replace() function will replace text within a part of a string. As the name suggest, it works similarly as its name.
The substr_replace() is a built-in function in PHP. The index from the input string in which replacement has to be done needs to be passed in start parameter. Replacement tells the new string. If needed, we can also specify the length up to which replacement is needed.
In case of, an array of strings is passed as a parameter then replacements will occur on each string.
If the start parameter is a negative number and length is less than or equal to start, length becomes 0.
This will return string or an array with replaced values depending upon the input values.
[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
Version, Description
PHP 5.0 , The count parameter was added.
PHP 4.0.5, From this version most of the parameters can be an array.
[/table]
<?php $strExample = 'TUTORIALWITHUS'; echo "String is : $strExample<hr />\n"; /* These two examples replace all of $strExample with 'mines'. */echo substr_replace($strExample, 'mines', 0) . "<br />\n"; echo substr_replace($strExample, 'mines', 0, strlen($strExample)) . "<br />\n"; ?>
In above example ,We have a string “TUTORIALWITHUS”. Now this function will replace the value in the var $strExample with value ‘mines’. Output of above code is as below:
<?php $strExample = 'TUTORIALWITHUS'; echo "String is : $strExample<hr />\n"; /* Insert 'mines' right at the beginning of $strExample. */echo substr_replace($strExample, 'mines', 0, 0) . "<br />\n"; ?>
In above example ,We have a string “TUTORIALWITHUS”. Now this function will insert ‘mines’ at the beginning of string in var $strExample with value ‘mines’. Output of above code is as below:
<?php $strExample = 'TUTORIALWITHUS'; echo "String is : $strExample<hr />\n"; /* These next two replace 'WITHUS' in $strExample mines 'mines'. */echo substr_replace($strExample, 'mines', 10, -1) . "<br />\n"; echo substr_replace($strExample, 'mines', -7, -1) . "<br />\n"; ?>
In above example ,We have a string “TUTORIALWITHUS”. Now this function will replace the value in the var $strExample with value ‘mines’. Output of above code is as below:
<?php $strExample = 'TUTORIALWITHUS'; echo "String is : $strExample<hr />\n"; /* Delete 'WITHUS' from $strExample. */echo substr_replace($strExample, '', 10, -1) . "<br />\n"; ?>
In above example ,We have a string “TUTORIALWITHUS”. Now this function will replace the value in the var $strExample with value ‘mines’. Output of above code is as below:
<?php $replace = array("month-1","month-2","month-3"); echo implode("<br>",substr_replace($replace,'string',0,5)); ?>
In above example ,We have an array of strings “month-1″,”month-2″,”month-3”. Now this function will replace the value in array month-1 with “string-1”, month-2 with “string-2” value in the string. Output of above code in the browser is as below:
<?php $input1 = array('1-', '2-', '3-'); $replace1 = array("January","Febuary","March"); echo implode("<br>\n",substr_replace($input1, $replace1, 2,0)); ?>
Output of above code in the browser is as below:
1-January<br>
2-Febuary<br>
3-March
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…