转换UTF8文本以在URL中使用 [英] Converting UTF8 text for use in a URL
问题描述
我正在开发一个使用UTF8来显示非英文字符的国际站点.我还使用了包含项目名称的友好URL.显然,我不能在URL中使用非英文字符.
I'm developing an international site which uses UTF8 to display non english characters. I'm also using friendly URLS which contain the item name. Obviously I can't use the non english characters in the URL.
此转换是否有某种常见的做法?我不确定应该用哪个英文字符替换它们.有些很明显(如è到e),但有些我不熟悉的字符(如ß).
Is there some sort of common practice for this conversion? I'm not sure which english characters i should be replacing them with. Some are quite obvious (like è to e) but other characters I am not familiar with (such as ß).
推荐答案
我通常将 iconv()与'ASCII//TRANSLIT'选项.输入如下内容:
I normally use iconv() with the 'ASCII//TRANSLIT' option. This takes input like:
último año
并产生如下输出:
'ultimo a~no
然后我使用preg_replace()用破折号替换空白:
Then I use preg_replace() to replace white spaces with dashes:
'ultimo-a~no
...并删除不需要的字符,例如
... and remove unwanted chars, e.g.
[^a-z0-9-]
它对于阿拉伯语或中文可能没有用,但对西班牙语,法语或德语的效果很好.
It's probably useless with Arabic or Chinese but it works fine with Spanish, French or German.
这篇关于转换UTF8文本以在URL中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!