如何在 PHP 中应用 URL 规范化规则? [英] How do I apply URL normalization rules in PHP?

查看:31
本文介绍了如何在 PHP 中应用 URL 规范化规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 中是否存在用于 URL 规范化的预先存在的函数或类?

Is there a pre-existing function or class for URL normalization in PHP?

具体来说,遵循这篇维基百科关于 URL 规范化的文章中规定的语义保留规范化规则,(或我应该遵循的任何标准").

Specifically, following the semantic preserving normalization rules laid out in this wikipedia article on URL normalization, (or whatever 'standard' I should be following).

  • 将方案和主机转换为小写
  • 转义序列中的字母大写
  • 添加尾随/(添加到目录,而不是文件)
  • 删除默认端口
  • 删除点段

现在,我想我只使用 parse_url(),并单独应用规则,但我更愿意避免重新发明轮子.

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.

推荐答案

Pear Net_URL2 库看起来至少可以完成您想要的部分任务.它将删除点段,修复大写并摆脱默认端口:

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:

include("Net/URL2.php");
$url = new Net_URL2('HTTP://example.com:80/a/../b/c');
print $url->getNormalizedURL();

发射:

http://example.com/b/c

我怀疑是否存在向目录添加尾部斜杠的通用机制,因为您需要一种将 url 映射到目录的方法,而这对于以通用方式进行是具有挑战性的.但它很接近.

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.

参考文献:

这篇关于如何在 PHP 中应用 URL 规范化规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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