如何去除重音并将字母变成“普通"ASCII 字符? [英] How to remove accents and turn letters into "plain" ASCII characters?

查看:20
本文介绍了如何去除重音并将字母变成“普通"ASCII 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从字符串中删除重音的最有效方法是什么,例如ÈâuÑ 变成 Eaun?

是否有我遗漏的简单内置方式或正则表达式?

解决方案

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

这篇关于如何去除重音并将字母变成“普通"ASCII 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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