如何在PHP中支持UTF8(日语,阿拉伯语,西班牙语...)URL [英] how to support UTF8 (japanese, arabic, spanish, ...) URL's in PHP

查看:85
本文介绍了如何在PHP中支持UTF8(日语,阿拉伯语,西班牙语...)URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Web应用程序,我们需要链接到一些用户生成的内容. 用户输入标题,例如产品,我们会为该产品生成SEO友好网址:

For a web application, we need to link to some user generated content. A users types in a title for e.g. a product and we generate an SEO friendly url for that product:

像这样

title: a nice product

www.user.com/product/a-nice-product

title: أبجد هوز

www.user.com/product/أبجد هوز

问题是不支持这些外语URL,浏览器拒绝打开这些链接.我见过wordpress设置支持那种url,所以我想有可能做到这一点.

The problem is that those foreign language url's aren't supported and a browser refuses to open those links. I've seen wordpress setups support that kind of url's so I guess it's possible to do this.

有人知道我们应该如何在php中支持它吗?

Does anyone know how we should support this in php?

Wikipedia可以很好地处理此问题: http://ar.wikipedia.org

wikipedia handles this just fine: http://ar.wikipedia.org

推荐答案

尽管URL本身仅允许使用US-ASCII字符,但是您可以

Although the URL itself only allows US-ASCII characters, you can use Unicode characters in the URI path if you encode them with UTF-8 and then convert them in US-ASCII characters by using the percent-encoding:

内部以不同字符编码形式提供标识符的系统(例如EBCDIC)通常会将文本标识符的字符转换为UTF-8 [

A system that internally provides identifiers in the form of a different character encoding, such as EBCDIC, will generally perform character translation of textual identifiers to UTF-8 [STD63] (or some other superset of the US-ASCII character encoding) at an internal interface, thereby providing more meaningful identifiers than those resulting from simply percent-encoding the original octets.

因此,您可以执行以下操作(假设使用UTF-8):

So you can do something like this (assuming UTF-8):

$title = 'أبجد هوز';
$path = '/product/'.rawurlencode($title);
echo $path;  // "/product/%D8%A3%D8%A8%D8%AC%D8%AF%20%D9%87%D9%88%D8%B2"

尽管URI路径实际上是用百分比编码来编码的,但是当使用UTF-8时,大多数现代浏览器都会显示此序列以Unicode表示的字符.

Although the URI path is actually encoded with the percent-encoding, most modern browsers will display the characters this sequence represents in Unicode when UTF-8 is used.

这篇关于如何在PHP中支持UTF8(日语,阿拉伯语,西班牙语...)URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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