且构网

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

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

更新时间:2023-02-22 21:59:51

如果已安装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.))