$str='Hello to plus2net.com';
$search='Hello';
$replace='Welcome';
echo str_replace($search,$replace,$str);
Output is here Welcome to plus2net.com
Syntax str_replace(search,replace,main_string,count);
search
:replace
:main_string
:count
:
<?Php
$string="This is an example on how html is used in
developing user-friendly web sites. By using HTML we can
control the content of a page";
echo $string;
echo "<br><br>";
$string=str_replace("html","PHP",$string);
echo "<b>After replace</b> <br><br>$string";
?>
If you run the above code your will see result like this
This is an example on how html is used in developing user-friendly web sites. By
using HTML we can control the content of a page
This is an example on how PHP is used in developing user-friendly web sites. By
using HTML we can control the content of a page
Welcome to Plus2net.com to learn PHP, MYSQL, HTML, jquery and many more.
After replacement Welcome to plus2net.com to learn php, MySQL, html, JQuery and many more.
Here is the code
$contents="Welcome to Plus2net.com to learn PHP, MYSQL, HTML , jquery and many more.";
echo "Original String $contents";
$search=array("Plus2net.com","PHP","jquery","HTML","MYSQL");
$replace=array("plus2net.com","php","JQuery","html","MySQL");
$contents=str_replace($search,$replace,$contents);
echo "After replacement $contents";
$replace=array("plus2net.com","php","<span style='background-color: #FFFF00'>JQuery</span>","html","MySQL");
$str='Raju is a good boy and he has many good habits.';
$search='good';
$replace='bad';
echo str_replace($search, $replace, $str,$count);
echo "<br>Number of replacements = $count <br>";
Output is here Raju is a bad boy and he has many bad habits.
Number of replacements = 2
Read how search & replace is used to change content of a page
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.