Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive str_replace

Tags:

php

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);.

like image 657
Amit Pal Avatar asked Jun 04 '15 18:06

Amit Pal


People also ask

Which function does the case-sensitive version of the Str_replace function?

Use the str_replace() function to perform a case-sensitive search.

What is difference between Str_replace and Str_ireplace?

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.

Is str_ replace case-sensitive?

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.

Which string function can help us to perform a case-insensitive search and replace?

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.


1 Answers

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);
like image 114
Narendrasingh Sisodia Avatar answered Oct 16 '22 20:10

Narendrasingh Sisodia