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, 2027🔥, 0💬
Popular Posts:
How To Open Standard Output as a File Handle in PHP? If you want to open the standard output as a fi...
Where to find tutorials on Using Azure API Management Developer Portal? Here is a list of tutorials ...
How to register and get an application ID from Azure AD? The first step to use Azure AD is to regist...
Where to find tutorials on Python programming language? I want to learn Python. Here is a large coll...
How to create a navigation file like navigation.xhtml for an EPUB 3.0 book? At least one navigation ...