正则表达式也匹配重音字符 [英] regex to also match accented characters

查看:449
本文介绍了正则表达式也匹配重音字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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>$0</b>", $text);

echo $text;

显然,que不匹配qué。我如何改变呢?有没有办法让 preg_replace 忽略所有重音符?

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

必须匹配的字符(西班牙语) / p>

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:

这是我的 foo 文字,带有qué和其他重音字符。

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

而不是

这是我的 foo 文字重音字符。

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

推荐答案

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

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

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'

这篇关于正则表达式也匹配重音字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆