Home > Back-end >  How to make preg_match accent insensitive?
How to make preg_match accent insensitive?

Time:01-09

I want to make preg_match accent insensitive but it doesn't work.

$haystack = 'été';
$needle = 'ete';

if (preg_match('#'.$needle.'#ui', $haystack, $matches)) {
    echo $haystack.' matches.';
    var_dump($matches);
}

Any idea ?

CodePudding user response:

the modifier u allows to process the string with multibyte characters. But it does not allow transliteration of accents. You can use iconv() to handle this case.

$haystack = 'nous sommes en été.';
$needle   = 'ete';

$haystack = iconv('UTF-8', 'ASCII//TRANSLIT', $haystack);
if (preg_match('#' . $needle . '#ui', $haystack, $matches)) {
    var_dump($matches);
}

Output:

array(1) {
  [0] =>
  string(3) "ete"
}
  • Related