Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array_intersect case-insensitive and ignoring tildes

Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?

The array_intersect PHP function compares array elements with === so I do not 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);

Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).

Any solution ?

Thank you in advance

like image 427
Dayron Gallardo Avatar asked Nov 29 '22 15:11

Dayron Gallardo


2 Answers

$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
like image 181
hindmost Avatar answered Dec 01 '22 06:12

hindmost


<?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
)
like image 38
Alex Jurado - Bitendian Avatar answered Dec 01 '22 04:12

Alex Jurado - Bitendian