正则表达式也匹配重音字符

regex to also match accented characters

我有以下 PHP 代码:

$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>[=10=]</b>", $text);

echo $text;

显然,"que" 不匹配 "qué"。我该如何改变它?有没有办法让 preg_replace 忽略所有口音?

必须匹配的字符(西班牙语):

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

我不想在应用正则表达式之前替换所有重音字符,因为文本中的字符应该保持不变:

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

而不是

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

如果你想在替换字符串中使用捕获的文本,你必须在你的 $search 变量中使用字符 类(无论如何,你手动设置它):

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

以此类推

您可以尝试像这样定义一个数组:

$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]");
}

如果我没记错 PHP,那应该用重音形式的字符 class 替换您的元音 - 这将保持原位。它还可以让您更轻松地更改搜索字符串;您不必记住用字符 classes 替换元音。您只需记住在搜索字符串中使用非重音形式。

(如果我忘记了没有 foreach 的特殊语法,请评论并告诉我。)

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

这个和大写一样会抱怨你的请求。旁注:ñ replacemet 对我来说听起来无效,因为 'niño' 与 'nino'

完全不同

我最终使用的解决方案:

$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>[=10=]</b>", $text)."\n";