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

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

问题描述

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

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

具体来说,按照 Wikipedia关于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.

参考文献:

  • http://pear.php.net/package/Net_URL2
  • http://pear.php.net/package/Net_URL2/docs/latest/Net_URL2/Net_URL2.html

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

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