PHP: Replace umlauts with closest 7-bit ASCII equivalent in an UTF-8 string(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"等价物替换它们.
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 等价物替换变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP:用 UTF-8 字符串中最接近的 7 位 ASCII 等价物替换变音符号
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01