如何在带有urlenocde的URL中用连字符(-)替换+ [英] How to replace + with hyphen (-) in URL with urlenocde
本文介绍了如何在带有urlenocde的URL中用连字符(-)替换+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据库中用空格存储了产品名称.例如Bike 86,洗衣机等.
使用urlencode后,URL进入浏览器,如下所示
I have stored the product names in database with spaces. For example Bike 86, Washing Machine etc.
After using urlencode URL comes to browser as below
http://www.example.com/product.php?productname=Washing+Machine
但我想得到这样的
But I want to get it such like
http://www.example.com/prodcut.php?productname=Washing-Machine
如何更改将空格编码为连字符(-)而不是"+"符号的urlencode函数.
如果urlencode无法实现,那我该怎么办.
注意:请给我一些其他解决方案,而不是str_replace();
How can I change the urlencode function that it encode the space into hyphen(-) not into "+" sign.
If it is not possible with urlencode then how can I do that.
Note: Please give me some other solution rather than str_replace();
推荐答案
这要复杂得多.
URL中的+具有特殊含义. Web服务器会将其解释为空间.如今,将空间编码为十六进制形式的最佳做法被认为是最佳做法,例如http://www.example.com/product.php?productname=Washing%20Machine
遵循RFC 3986标准,但是+将会存在很长一段时间,并且仍然受所有浏览器和服务器的支持.
您要用-替换+.您不能这样做(即使使用str_replace也是如此),因为+表示与空格相同,而-表示不相同.就是-.
虽然仍然有可能,但是在PHP中却没有.您将需要配置Web服务器以重写任何-在查找文件之前它会看到空格的迹象.在Apache上,您很可能会在网站的根目录中创建一个名为.htaccess
的文件,该文件将包含以下内容:
It''s a bit more complicated than that.
The + in the URL has a special meaning. The web server will interpret it as a space. These days it is considered best practices to encode space in its hex form e.g.http://www.example.com/product.php?productname=Washing%20Machine
which follows RFC 3986 standards, but the + will be around for a long time and is still supported by all browsers and servers.
You want to replace the + with -. You can''t do that (even with str_replace) because whereas + means the same thing as space, - doesn''t. It just means -.
It is still possible though, but not in PHP. You will need to configure the web server to rewrite any - signs that it sees to spaces before it looks for the file. On Apache you would most likely create a file called .htaccess
in the root directory of your site and it would contain something like:
RewriteEngine on
# Replace the minus character in incoming URLs with a space.
RewriteRule ^([^-]*)-([^-]*-.*)
1%20
2 [N] RewriteRule ^([^-] *)-([^-] *)
2 [N] RewriteRule ^([^-]*)-([^-]*)
这篇关于如何在带有urlenocde的URL中用连字符(-)替换+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文