PHP:用UTF-8字符串中最接近的7位ASCII等效字符替换变音符号 [英] PHP: Replace umlauts with closest 7-bit ASCII equivalent in an UTF-8 string

查看:92
本文介绍了PHP:用UTF-8字符串中最接近的7位ASCII等效字符替换变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是从字符串中删除所有重音和变音符,将lärm"变成"larm",或者将andré"变成"andre".我想做的是先对字符串进行utf8_decode,然后在其上使用strtr,但是由于我的源文件另存为UTF-8文件,因此我无法输入所有变音符号的ISO-8859-15字符-编辑器将插入UTF-8字符.

What I want to do is to remove all accents and umlauts from a string, turning "lärm" into "larm" or "andré" into "andre". What I tried to do was to utf8_decode the string and then use strtr on it, but since my source file is saved as UTF-8 file, I can't enter the ISO-8859-15 characters for all umlauts - the editor inserts the UTF-8 characters.

显然,解决此问题的方法是拥有一个包含ISO-8859-15文件的包含文件,但是必须有比拥有另一个所需包含文件更好的方法?

Obviously a solution for this would be to have an include that's an ISO-8859-15 file, but there must be a better way than to have another required include?

echo strtr(utf8_decode($input), 
           'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ',
           'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');

更新:也许我对尝试做的事情有点不准确:我实际上并不希望删除变音符号,而是将其替换为最接近的一个字符ASCII". /p>

UPDATE: Maybe I was a bit inaccurate with what I try to do: I do not actually want to remove the umlauts, but to replace them with their closest "one character ASCII" equivalent.

推荐答案

iconv("utf-8","ascii//TRANSLIT",$input);

扩展了示例

这篇关于PHP:用UTF-8字符串中最接近的7位ASCII等效字符替换变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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