编程语言
首页 > 编程语言> > PHP array_intersect不区分大小写并忽略波浪号

PHP 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函数中,建议使用红色(正常原因===).

有解决方案吗

先感谢您

解决方法:

<?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
)

标签:php,case-insensitive,string-comparison,array-intersect
来源: https://codeday.me/bug/20190713/1446403.html