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, 1858🔥, 0💬
Popular Posts:
How To Move Uploaded Files To Permanent Directory in PHP? PHP stores uploaded files in a temporary d...
How to create the Hello-3.0.epub package? I have all required files to create Hello-3.0.epub. To cre...
How to login to Azure API Management Publisher Dashboard? If you have given access permission to an ...
What is Azure API Management Publisher Dashboard? Azure API Management Publisher Dashboard is an Azu...
How to use the urllib.request.Request object to build more complex HTTP request? The urllib.request....