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.
⇒ Reformatting a Paragraph of Text in PHP
2016-10-13, ∼3267🔥, 0💬
Popular Posts:
How to Install Docker Desktop on Windows 10? You can follow this tutorial to Install Docker Desktop ...
How to build a test service operation to dump everything from the "context.Request" object in the re...
How to use the RSS Online Validator at w3.org? You can follow this tutorial to learn how to use the ...
How to use "link" command tool to link objet files? If you have object files previously compiled by ...
How to make application release build with Visual Studio 2017? If you want to make a final release b...