Categories: PHP

str_ireplace – PHP String Functions

Syntax :

str_ireplace ( find, replace, string, count );

Description :

str_ireplace() function will replace all occurrences of the search string with the replacement string. It is case – insensitive function.

This function works by the following rules:

  • If the string to be searched is an array, it returns an array
  • If the string to be searched is an array, find and replace is performed with every array element
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
  • If find is an array and replace is a string, the replace string will be used for every find value
Note : This function is binary-safe..
Note : This function is case-insensitive. Use the str_replace() function to perform a case-sensitive search.

Parameter :

  • find – This is a Required parameter. It sets values to find.
  • replace – This is a Required parameter. It sets values to replace.
  • string – This is a Required parameter. It sets string to search.
  • count – This is an Optional parameter. If passed, this will be set to the number of replacements performed.

Output :

This will return string or an array with replaced values.


ChangeLog :

[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
Version, Description
PHP 5.0 , The count parameter was added.

[/table]


Related articles : str_replace(), preg_replace, strtr().


str_ireplace() – PHP Functions Example 1 :
<?php
// below statement will replace the Hi word with Hello."
$strExample = "Hi from tutorialmines!"; 
echo str_ireplace("Hi","Hello",$strExample);
?>

In above example ,We have a string “Hi from tutorialmines! “. Now this function will replace the value “Hi” with “Hello” value in the string. Output of above code in the browser is as below:

Hello from tutorialmines!

str_ireplace() – PHP Functions Example 2 :
<?php
$arr = array("january","febuary","march","april");
print_r(str_ireplace("Febuary","Month-2",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>

In above example ,We have an array of strings “january”,”febuary”,”march”,”april”. Now this function will replace the value in array “Febuary” with “Month-2” value in the string. Output of above code in the browser is as below:

Array ( [0] => january [1] => Month-2 [2] => march [3] => april ) Replacements: 1

str_ireplace() – PHP Functions Example 3 :  less elements in replace than find
<?php
$strArr = array("january","febuary","march","april");
$find = array("januaRy","Febuary",""); // Case-insensitive
$replace = array("first replacement","second replacement");
print_r(str_ireplace($find,$replace,$strArr,$count));
echo "<br/>Numbers of replacements done are - ".$count;
?>

In above example ,We have an array of strings “january”,”febuary”,”march”,”april”.

Now this function will find the value in array “januaRy”,”Febuary”,””. and replace them with “”first replacement”,”second replacement”” values. Output of above code in the browser is as below:

Array ( [0] => first replacement [1] => second replacement [2] => march [3] => april )
Numbers of replacements done are – 2


jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago