且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何去除重音并将字母变成“普通"ASCII 字符?

更新时间:2023-02-22 22:26:00

如果你安装了 iconv,试试这个(示例假设你的输入字符串是 UTF-8):

echo iconv('UTF-8', 'ASCII//TRANSLIT', $string);

(iconv 是一个可以在各种编码之间进行转换的库;它非常高效,并且默认包含在许多 PHP 发行版中.最重要的是,与尝试推出自己的解决方案相比,它绝对更容易且更防错(你知道吗?有一个带卷曲的拉丁字母 N"?我都没有.))>

What is the most efficient way to remove accents from a string e.g. ÈâuÑ becomes Eaun?

Is there a simple, built in way that I'm missing or a regular expression?

If you have iconv installed, try this (the example assumes your input string is in UTF-8):

echo iconv('UTF-8', 'ASCII//TRANSLIT', $string);

(iconv is a library to convert between all kinds of encodings; it's efficient and included with many PHP distributions by default. Most of all, it's definitely easier and more error-proof than trying to roll your own solution (did you know that there's a "Latin letter N with a curl"? Me neither.))