PHP ignores case sensitivity when comparing array values

I need to change the code in the application I'm working on using the array_diff method ($ array1, $ array2). The problem I am facing is case sensitivity, and I need to return the correct value if the array values ​​match, even if the case is different. I do not want to change the case to lower case, because I need to return a value in order to keep its case. I am a little confused as the best method for this.

+4
source share
3 answers

You need: array_udiff and strcasecmp

$result = array_udiff($arr1, $arr2, 'strcasecmp'); 

eg.

 <?php $arr1 = array("string","string","string"); $arr2 = array("String","string","sTRING"); $result = array_udiff($arr1, $arr2, 'strcasecmp'); print_r($result); ?> 

$result should reflect array ( )

+10
source

Use

strcasecmp - strcasecmp - strcasecmp binary safe string comparison

 <?php $var1 = "Hello"; $var2 = "hello"; if (strcasecmp($var1, $var2) == 0) { echo '$var1 is equal to $var2 in a case-insensitive string comparison'; } ?> 

See this link for more details.

0
source

Serialization can help, so you can use strcasecmp in the resulting lines:

 <?php $arr1 = array("string","string"); $arr2 = array("String","sTRING"); $equal = (strcasecmp(serialize($arr1), serialize($arr2)) === 0); ?> 
0
source

Source: https://habr.com/ru/post/1304539/


All Articles