我知道这里的问题似乎是通过webbiedave解决的,但是我有自己的问题。
第一个问题:编码字符不区分大小写。所以%C3和%c3都是完全相同的字符,尽管它们与URI不同。所以这两个URI指向相同的位置。
问题二:文件夹%20(2)和文件夹的%20%282%29都是有效的URI urlencoded进行,这点到相同的位置,尽管它们是不同的URI。
第三个问题:如果我摆脱了url编码字符,我有两个位置具有相同的URI,如bla%2Fblubb和bla/blubb。
那么该怎么做呢?为了比较两个URI,我需要以这种方式对它们进行规范化,以便将它们分解到所有组件中,对所有路径和查询部分进行一次url解码,rawurlencode并将它们粘合在一起,然后我可以对它们进行比较。
而且这可能是功能正常化它:
function normalizeURI($uri) {
$components = parse_url($uri);
$normalized = "";
if ($components['scheme']) {
$normalized .= $components['scheme'] . ":";
}
if ($components['host']) {
$normalized .= "//";
if ($components['user']) { //this should never happen in URIs, but still probably it's anything can happen thursday
$normalized .= rawurlencode(urldecode($components['user']));
if ($components['pass']) {
$normalized .= ":".rawurlencode(urldecode($components['pass']));
}
$normalized .= "@";
}
$normalized .= $components['host'];
if ($components['port']) {
$normalized .= ":".$components['port'];
}
}
if ($components['path']) {
if ($normalized) {
$normalized .= "/";
}
$path = explode("/", $components['path']);
$path = array_map("urldecode", $path);
$path = array_map("rawurlencode", $path);
$normalized .= implode("/", $path);
}
if ($components['query']) {
$query = explode("&", $components['query']);
foreach ($query as $i => $c) {
$c = explode("=", $c);
$c = array_map("urldecode", $c);
$c = array_map("rawurlencode", $c);
$c = implode("=", $c);
$query[$i] = $c;
}
$normalized .= "?".implode("&", $query);
}
return $normalized;
}
现在你可以改变webbiedave的功能,这一点:
function uriMatches($uri1, $uri2) {
return normalizeURI($uri1) === normalizeURI($uri2);
}
这应该做的。是的,它比我想要的要复杂得多。
哈。多么简单明显的解决方案!不错。 +1 – 2010-10-08 21:59:28