Regex to combine 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 this? Is there any way to preg_replaceignore all accents?

Characters to match (Spanish):

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

I do not want to replace all characters with an accent before applying the regular expression, because the characters in the text must remain unchanged:

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

but not

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

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

. : ñ , "niño" "nino"

0

, $search ( ):

$search = "foo bar qu[eé]"

.

+1

, , , :

$search_for_preg = str_ireplace(["e","a","o","i","u","n"],
                                ["[eé]","[aá]","[oó]","[ií]","[uú]","[nñ]"],
                                $search_string);

$text = preg_replace("/$search_for_preg/iu", "<b>$0</b>", $text)."\n";
+1

:

$vowel_replacements = array(
    "e" => "eé",
    // Other letters mapped to their other versions
);

preg_match - :

foreach ($vowel_replacements as $vowel => $replacements) {
    str_replace($search_string, "$vowel", "[$replacements]");
}

If I remember my right to PHP, this should replace your vowels with the character class of their accented forms - which will keep it in place. It also allows you to easily change the search bar; You do not need to forget about replacing vowels with character classes. All you need to remember is to use the non-accumulated form in the search bar.

(If there is any special syntax, I forget that it does this without foreach, comment and let me know.)

0
source

All Articles