PHP array_intersect case insensitive and ignores tildes

Is there any function like "array_intersect", but it is case insensitive and ignores tildes?

PHP's array_intersect function compares array elements with ===, so I don't get the expected result.

For example, I want this code:

$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);

Conclusion gréen and red . In the default function, array_intersect only suggests red (normal reason ===).

Any solution?

Thank you in advance

+3
source share
2 answers
<?php

function to_lower_and_without_tildes($str,$encoding="UTF-8") {
  $str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
  return $str;
}

function compare_function($a,$b) {
  return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}

$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);

output:

Array
(
    [a] => gréen
    [0] => red
)
+5
source
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
+14
source

All Articles