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.
<?
$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 "
";
$string=str_replace("html","PHP",$string);
echo "[b]After replace[/b]
$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
So 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
The main difference between str_replace and str_ireplace is str_replace is a case sensitive string replacement and str_ireplace is a case in-sensitive string search and replacement.
str_ireplace function in PHP when applied to a main string, it searches for occurrence of a string and replace them by another string.
This function can take an array as search string and each element of the array if present is replaced by another string. We can try with an example. If you are searching for a simple example then you can see str_replace function. Here is a sample code which takes an array of search words and replaces them by another word within a string.
$search = array("good", "php", "html", "ASP");
$final_string = str_replace($search, "*", "All good sites uses PHP along with HTML or they use ASP and html");
echo $final_string;
As we have seen here we can use an array as search terms and all the elements of the array can be replaced by a replace string. We can also use another array as replacement array and each element is replace by matching element from replacement array, if not element is found then blank string is replaced. Here is the script code for that
$search = array("good", "php", "html", "asp");
$replace=array("GOOD","PHP","HTML","ASP","NO");
$final_string = str_replace($search, $replace, "All good sites uses PHP along with HTML or they use asp and html");
echo $final_string;
Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!
SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…