PHP 比较带重音和不带重音的字符串 [英] PHP compare string with accents and without accents
问题描述
是否可以在 PHP 中比较这些字符串:
Is it possible in PHP to compare thoses strings:
Æther == AEther == Aether
我想从这个等价中得到一个积极的结果
I'd like to get a positive result from this equivalence
我实际上尝试了多种方法,但都没有真正成功:
I've actually tried multiple things but without real success:
用 strtr 将 Æ 和任何特殊字符替换为 Ae(性能不佳,我宁愿保持字符串不变)
Replacing the Æ and any special character to Ae with strtr (bad performance and I would rather keep the string as is)
使用 strcmp/strcasecmp,这解决了大写问题,但我仍然无法处理所有 UTF-8 字符
Using strcmp/strcasecmp, this solve the caps problem but I still have trouble with all UTF-8 characters
我想要实现的是解析从 json 中检索到的元素列表并与其他一些 json 文件匹配,有些可以拼写不同(utf8 或非 utf8、caps 等),因此,现在,我发现这样做的唯一方法是制作第三个这样的json:
What I'm trying to achieve it's to parse a list of elements retrieved from a json and match with some other json file, some can be spelled differently (utf8 or non utf8, caps etc.) and so, for now, the only way I found to do this it's to make a third json like this:
{
"match": {
"name": "Unravel the \u00c6ther"
},
"replace": {
"name": "Unravel the aether"
}
我用正确的字符串替换了基本字符串,但我想找到一种方法来自动化这个过程.
And I replace the base string with the corect one, but I'd like to find a way to automatise the process.
推荐答案
可以使用 iconv 的音译功能:
You can use iconv's transliterate feature:
iconv('utf8', 'ASCII//TRANSLIT', 'Æther') == 'Aether';
某些 Windows 系统可能需要使用utf-8"而不是utf8".
Some Windows systems may required the use of 'utf-8' instead of 'utf8'.
这篇关于PHP 比较带重音和不带重音的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!