PHP array_intersect不区分大小写并忽略波浪号

时间:2014-02-11 11:47:01

标签: php string-comparison case-insensitive array-intersect

是否有任何类似于“array_intersect”的函数,但它处于模式不区分大小写并且忽略了波浪线?

array_intersect PHP函数将数组元素与===进行比较,所以我没有得到预期的结果。

例如,我想要这段代码:

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

输出gréen红色。在默认的array_intersect函数中,建议 red (正常原因===)。

任何解决方案?

提前谢谢

2 个答案:

答案 0 :(得分:12)

$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));

答案 1 :(得分:4)

<?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 to_lower_and_without_tildes($a)===to_lower_and_without_tildes($b)?0:1;
}

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

输出:

Array
(
    [a] => gréen
    [0] => red
)