phpregexcharacternon-ascii-charactersaccent-insensitive

regex to also match accented characters


I have the following PHP code:

$search = "foo bar que";
$search_string = str_replace(" ", "|", $search);

$text = "This is my foo text with qué and other accented characters.";
$text = preg_replace("/$search_string/i", "<b>$0</b>", $text);

echo $text;

Obviously, "que" does not match "qué". How can I change that? Is there a way to make preg_replace ignore all accents?

The characters that have to match (Spanish):

á,Á,é,É,í,Í,ó,Ó,ú,Ú,ñ,Ñ

I don't want to replace all accented characters before applying the regex, because the characters in the text should stay the same:

"This is my foo text with qué and other accented characters."

and not

"This is my foo text with que and other accented characters."


Solution

  • $search = str_replace(
       ['a','e','i','o','u','ñ'],
       ['[aá]','[eé]','[ií]','[oó]','[uú]','[nñ]'],
       $search)
    

    This and the same for upper case will complain your request. A side note: ñ replacemet sounds invalid to me, as 'niño' is totaly diferent from 'nino'