如何在 PHP 中应用 URL 规范化规则?
PHP 中是否有用于 URL 规范化的预先存在的函数或类?
具体来说,遵循关于 URL 规范化的维基百科文章中规定的语义保留规范化规则,(或其他内容)我应该遵循的“标准”)。
- 将方案和主机转换为小写
- 将转义序列中的字母大写
- 添加尾随 / (到目录,而不是文件)
- 删除默认端口
- 删除点段
现在,我想我只使用 parse_url()
,并单独应用规则,但我宁愿避免重新发明轮子。
Is there a pre-existing function or class for URL normalization in PHP?
Specifically, following the semantic preserving normalization rules laid out in this wikipedia article on URL normalization, (or whatever 'standard' I should be following).
- Converting the scheme and host to lower case
- Capitalizing letters in escape sequences
- Adding trailing / (to directories, not files)
- Removing the default port
- Removing dot-segments
Right now, I'm thinking that I'll just use parse_url()
, and apply the rules individually, but I'd prefer to avoid reinventing the wheel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Pear Net_URL2 库看起来至少可以完成您想要的部分功能。它将删除点段,修复大写并摆脱默认端口:
发出:
我怀疑是否有一种通用机制可以向目录添加尾部斜杠,因为您需要一种将 url 映射到目录的方法,这在通用中很难做到方式。但已经很接近了。
参考文献:
The Pear Net_URL2 library looks like it'll do at least part of what you want. It'll remove dot segments, fix capitalization and get rid of the default port:
emits:
I doubt there's a general purpose mechanism for adding trailing slashes to directories because you need a way to map urls to directories which is challenging to do in a generic way. But it's close.
References: