str_ireplace(): case in-sensitive String Replace

$search = "php";
$replace="JavaScript";
$final_string = str_ireplace($search, $replace, "Sites uses PHP along with HTML");
echo $final_string;
Output is here
Sites uses JavaScript along with HTML
Syntax
str_ireplace(search,replace,main_string,count);
search:
Required: String to be searched for matching, can be an array
replace:
Required: String to be replaced if found using search string, can be an array
main_string:
Required : String on which search and replace to be applied, can be an array
count:
Optional: Stores the number of replacements performed.


Output is a string or array after performing search and replace over the input string

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.

Search Array & replace 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;
Output is here
All * sites uses PHP along with HTML or they use * and *

Search array & replace array

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("server", "php", "JSP", "asp");
$replace=array("client","HTML","JavaScript","CSS");
$final_string = str_ireplace($search, $replace, " Server side scripts are jsp, PHP and ASP ");
echo $final_string;
Output is here
client side scripts are JavaScript, HTML and CSS

Using optional parameter count to know the number of replacements

Above script can be modified to add optional parameter $count. This variable will store the number of search and replacement carried out.
$search = array("server", "php", "JSP", "asp");
$replace=array("client","HTML","JavaScript","CSS");
$final_string = str_ireplace($search, $replace, " Server side scripts are jsp, PHP and ASP ",$count);
echo $final_string;
echo "<br>Number of replacement= $count ";
Output is here
client side scripts are JavaScript, HTML and CSS 
Number of replacement= 4

String Functions Case sensitive string replace
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    John Komla

    19-09-2009

    This tutorial is 1000 times helpful to me. Thanks too much. But is it possible to show us how you built this form we are posting comments from using Ajax ? just like it is working now ? I mean insert records to database without page refresh using Ajax POST method. Thank you, John
    smo

    20-09-2009

    Just read comment posting tutorial. But the Ajax based script is not yet available to download. You can download the basic one.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer