How do I use str_replace
but to be case-insensitive when it searches for the string?
For example, suppose i want to replace ABcD
with a
. The resulting command would be $string=str_replace("ABcD","a",$string);
. But if there is some string like "abCD" then again I have to use $string=str_replace("abCD","a",$string);
.
Use the str_replace() function to perform a case-sensitive search.
The difference is that the str_replace() function is case-sensitive whereas str_ireplace() is not. Parameters: This function accepts four parameters out of which 3 are mandatory and 1 is optional.
PHP str_replace is case-sensitive. If you need to perform a case-insensitive search, try str_ireplace() function. If the variable specified by the third argument is an array, the function will check every array element and will return an array.
sub() In this, sub() of the regex is used to perform the task of replacement, and IGNORECASE ignores the cases and performs case-insensitive replacements.
Then instead of using str_replace
try usingstr_ireplace
Its a case insensitive version of str_replace
so use it as
$string = str_ireplace("ABcD","a",$string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With