PHP case-insensitive in_array function
you can use preg_grep()
:
$a= array(
'one',
'two',
'three',
'four'
);
print_r( preg_grep( "/ONe/i" , $a ) );
How can I get in_array() case-insensitive?
Well if you can make sure, that the search word is always in lowercase, just also put the array in lower case by looping through all values with array_map()
and putting them in lowercase with strtolower()
, e.g.
if (in_array('lookupvalue', array_map("strtolower", $array))) {
// do something
}
Case-insensitive array search
array_search(strtolower($search), array_map('strtolower', $array));
array_keys or in_array insensitive doesnt work correctly
Create the array and find the keys with with strtolower
:
$wordsExample = array("example1","example2","example3","August","example4");
$lowercaseWordsExample = array();
foreach ($wordsExample as $val) {
$lowercaseWordsExample[] = strtolower($val);
}
if(in_array(strtolower('august'),$lowercaseWordsExample,FALSE))
return "WOHOOOOO";
if(in_array(strtolower('aUguSt'),$lowercaseWordsExample,FALSE))
return "WOHOOOOO";
Another way would be to write a new in_array
function that would be case insensitive:
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
If you want it to use less memory, better create the words array using lowercase letter.
Related Topics
Ssl Operation Failed with Code 1: Dh Key Too Small
How to Easily Consume a Web Service from PHP
PHP Case-Insensitive In_Array Function
What's the Difference Between PHP's Dom and Simplexml Extensions
What Does Filter_Sanitize_String Do
PHP Parse Error: Syntax Error, Unexpected T_Object_Operator
File_Get_Contents() Give Me 403 Forbidden
Why Should I Fix E_Notice Errors
Casting an Array with Numeric Keys as an Object
Can't Pass MySQLi Connection in Session in PHP
Getting a Modified Preorder Tree Traversal Model (Nested Set) into a <Ul>
How to Separate Date and Time from Datetime in MySQL
Advantages/Disadvantages of Pconnect Option in Codeigniter
Dom Parser That Allows HTML5-Style </ in <Script> Tag