問題描述
我想要做的是從字符串中刪除所有重音和變音符號,將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),
'???????¥μàá??????èéê?ìí??D?òó????ùú?üY?àáa?????èéê?ìí??e?òó????ùú?üy?',
'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 等價物替換變音符號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!