PHP str_ireplace() Function
In this chapter you will learn:
- Definition for PHP str_ireplace() Function
- Syntax for PHP str_ireplace() Function
- Parameter for PHP str_ireplace() Function
- Return for PHP str_ireplace() Function
- Example - Replace the characters "WORLD" (case-insensitive) in the string "Hello world from java2s.com!" with "PHP"
- Example - Using str_replace() with an array and a count variable
- Example - Using str_replace() with less elements in replace than find
Definition
The str_ireplace() function replaces some characters with some other characters in a string.
This function is case-insensitive. Use the str_replace() function to perform a case-sensitive search.
Syntax
PHP str_ireplace() Function has the following syntax.
str_ireplace(find,replace,string,count)
Parameter
Parameter | Is Required | Description |
---|---|---|
find | Required. | Value to find |
replace | Required. | Value to replace the value in find |
string | Required. | String to be searched |
count | Optional. | A variable that counts the number of replacements |
If search and replace are arrays, then str_ireplace() takes a value from each array and uses them to search and replace on subject.
If replace has fewer values than search, then an empty string is used for the rest of replacement values.
Return
Returns a string or an array with the replaced values
Example
Replace the characters "WORLD" (case-insensitive) in the string "Hello world from java2s.com!" with "PHP":
<?php
echo str_ireplace("WORLD","PHP","Hello world from java2s.com!");
?>
The code above generates the following result.
Example 2
Using str_replace() with an array and a count variable:
<?php//from j a va 2 s . c o m
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>
The code above generates the following result.
Example 3
Using str_replace() with less elements in replace than find:
<?php//j a v a 2 s . c o m
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP str_pad() Function
- Syntax for PHP str_pad() Function
- Parameter for PHP str_pad() Function
- Return for PHP str_pad() Function
- Example - Padding on both side
- Example - Padding with specified character
- Example - Set which side to pad