PHP case sensitive String Replace

To replace a part of the string with another string we will use str_replace function in PHP. This function will replace all the occurrence of the string we target and will replace with the string we supply. Here is an example.

<?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

After replace

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

Here you can see the text html is replace by PHP but HTML is not replaced.
So str_replace() is a case sensitive string replacing function. We can use str_ireplace() to get case insensitive string replacement

Find & Replace

We can create an array of search strings and an array or replace strings. These two array we can apply to search and replace words from the content. The matching words will be replace by matching strings from replace array. Original String
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";

Highlighting the changes.

Replace the replace array with additional tags to highlight the replaced string.
$replace=array("plus2net.com","php","<span style='background-color: #FFFF00'>JQuery</span>","html","MySQL");
Read how search & replace is used to change content of a page

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Your Rating


Google+
PHP String Functions

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2016 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer