regex to also match accented characters

為{幸葍}努か 提交于 2019-12-05 14:52:47
$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'

If you want to use the captured text in the replacement string, you have to use character classes in your $search variable (anyway, you set it manually):

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

And so on.

The solution I finally used:

$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";

You could try defining an array like this:

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

Then, before your preg_match call, do something like this:

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

If I'm remembering my PHP right, that should replace your vowels with a character class of their accented forms -- which will keep it in place. It also lets you change the search string far more easily; you don't have to remember to replaced the vowels with their character classes. All you have to remember is to use the non-accented form in your search string.

(If there's some special syntax I'm forgetting that does this without a foreach, please comment and let me know.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!