对于网络应用程序,我们需要链接到一些用户生成的内容。 用户输入标题,例如一个产品,我们为该产品生成一个 SEO 友好的 url:
像这样
title: a nice product
www.user.com/product/a-nice-product
title: أبجد هوز
www.user.com/product/أبجد هوز
问题是这些外语网址不受支持,并且浏览器拒绝打开这些链接。我已经看到 WordPress 设置支持这种 url,所以我想这是可能的。
有谁知道我们应该如何在 php 中支持这个?
维基百科处理得很好:http://ar.wikipedia.org
最佳答案
虽然 URL 本身只允许 US-ASCII 字符,但您可以 use Unicode characters in the URI path如果您使用 UTF-8 对其进行编码,然后使用 percent-encoding 将它们转换为 US-ASCII 字符:
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):
$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 表示的字符。
关于php - 如何在 PHP 中支持 UTF8(日语、阿拉伯语、西类牙语等)URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3907995/