Tools, FAQ, Tutorials:
Replacing a Substring in PHP
How To Replace a Substring in a Given String?
✍: FYIcenter.com
If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():
<?php $string = "Warning: System will shutdown in NN minutes!"; $pos = strpos($string, "NN"); print(substr_replace($string, "15", $pos, 2)."\n"); sleep(10*60); print(substr_replace($string, "5", $pos, 2)."\n"); ?>
This script will print:
Warning: System will shutdown in 15 minutes! (10 minutes later) Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the end of the string.
2016-10-13, 736👍, 0💬
Popular Posts:
How to view API details on the Publisher Portal of an Azure API Management Service 2017 version? You...
Where to find tutorials on the 2017 version of API Management Services at Azure Portal? Here is a li...
What is EPUB 2.0 Metadata "dc:date" Element? EPUB 2.0 Metadata "dc:date" is an optional metadata ele...
What is Azure API Management Developer Portal? Azure API Management Developer Portal is an Azure Web...
Where to see resource detailed information of my API Management Service on Azure Portal? Once you ha...