I have an array that consists of terms, some of them contain accented characters. I do a preg grep like this
$data= array('Napoléon','Café');
$result = preg_grep('~' . $input . '~i', $data);
So if user type in 'le' I would also want the result 'Napoléon' to be matched, which does not work with the ablove command.
I did some searching and found that this function might be relevant
preg_match("/[\w\pL]/u",$var);
How can I combine these and make it work?
This is not possible with a regular expression pattern only. It is not because you can not tell the regex engine to match all "e" and similars. However, it is possible to first normalize the input data (both the array as well as the search input) and then search the normalized data but return the results for the non-normalized data.
In the following example I use transliteration to do this kind of normalization, I guess that is what you're looking for:
The exemplary output is:
The search function itself is rather straight forward as written above, transliterating the inputs, doing the
preg_grep
and then returning the original intputs matches:This code requires the
Transliterator
from the Intl extension, you can replace it with any other similar transliteration or translation function.I can not suggest to use
str_replace
here btw., if you need to fall-back to a translation table, usestrtr
instead. That is what you're looking for. But I prefer a library that brings the translation with it's own, especially if it's the Intl lib, you normally can't beat it.You can write something like this:
Actually you even don't need regex in this case, simple
strpos
will be enough.