PHP 标准化远程 URL
是否有任何快速函数可以将 HtTp://www.ExAmPle.com/blah
转换为 http://www.example.com/blah
基本上我想将 url 中不区分大小写的部分小写。
Is there any quick function that will convert: HtTp://www.ExAmPle.com/blah
to http://www.example.com/blah
Basically I want to lower case the case-insensitive parts of a url.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,您必须自己为其编写代码。
但是您可以使用
parse_url()
将 URL 拆分为多个部分。No, you'll have to write code for it on your own.
But you can use
parse_url()
to split the URL into its parts.既然你要求“快速”,这里有一个单行代码可以完成这项工作:
输出:
编辑/添加:
我已经测试过,这个版本比使用带有匿名的 preg_replace_callback 快大约 55%功能:
Since you asked for "quick," here's a one-liner that does the job:
Outputs:
EDIT/ADD:
I've tested, and this version is about 55% faster than using preg_replace_callback with an anonymous function:
我相信这个
类
将满足您的需求http://www.glenscott.co.uk/blog/2011/01/09/normalize-urls-with-php/I believe this
class
will do what you're looking for http://www.glenscott.co.uk/blog/2011/01/09/normalize-urls-with-php/这是一个解决方案,扩展了 @ThiefMaster 已经提到的内容:
DEMO
注意:未经实战测试,但它应该可以帮助您前进。
Here's a solution, expanding on what @ThiefMaster already mentioned:
DEMO
Note: Not battle-tested but it should get you going.