str_replace() performs case-sensitive literal replacement. Its optional fourth argument receives the number of replacements, which is useful when you need to know whether anything changed.
$text = 'red car, red bike';
$result = str_replace('red', 'blue', $text, $count);
echo $result; // blue car, blue bike
echo $count; // 2Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.
$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 & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.