绝对URL的相对+基本URL?

时间:2012-06-26 20:30:54

标签: php

基本上,给定像

这样的基本网址
file:///path/to/some/file.html

之类的相对网址
another_file.php?id=5

我想出去

file:///path/to/some/another_file.php?id=5

我找到this script(与this one相同),但它似乎不适用于file://方案。我在使用我的代码之前正在进行一些本地测试,所以我想同时处理file://http://

任何人都知道要执行此操作的脚本/函数吗?

在C#中,我使用Uri(Uri base, string rel)


以上只是一个例子。它应该适用于您可以投入<a href="xxx">任何网址。


这是迄今为止我所做的最好的,但它不会处理..以及其他一些事情:

function rel2abs($base, $rel) {
    if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
    if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
    $parse = parse_url($base);
    $path = preg_replace('#/[^/]*$#', '', $parse['path']);
    if ($rel[0] == '/') $path = '';
    $abs = (isset($path['host'])?$path['host']:'')."$path/$rel";
    $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
    for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
    return $parse['scheme'].'://'.$abs;
}

6 个答案:

答案 0 :(得分:2)

我已经改编了Puggan Se的答案来处理HTML页面中看到的某些相对URL。

function url2absolute($baseurl, $relativeurl) {

    // if the relative URL is scheme relative then treat it differently
    if(substr($relativeurl, 0, 2) === "//") {
        if(parse_url($baseurl, PHP_URL_SCHEME) != null) {
            return parse_url($baseurl, PHP_URL_SCHEME) . ":" . $relativeurl;
        } else { // assume HTTP
            return "http:" . $relativeurl;
        }
    }

    // if the relative URL points to the root then treat it more simply
    if(substr($relativeurl, 0, 1) === "/") {
        $parts = parse_url($baseurl);
        $return = $parts['scheme'] . ":";
        $return .= ($parts['scheme'] === "file") ? "///" : "//";
        // username:password@host:port ... could go here too!
        $return .= $parts['host'] . $relativeurl;
        return $return;
    }

    // If the relative URL is actually an absolute URL then just use that
    if(parse_url($relativeurl, PHP_URL_SCHEME) !== null) {
        return $relativeurl;
    }

    $parts = parse_url($baseurl);

    // Chop off the query string in a base URL if it is there
    if(isset($parts['query'])) {
        $baseurl = strstr($baseurl,'?',true);
    }

    // The rest is adapted from Puggan Se

    $return = ""; // string to return at the end
    $minpartsinfinal = 3; // for everything except file:///
    if($parts['scheme'] === "file") {
        $minpartsinfinal = 4;
    }

    // logic for username:password@host:port ... query string etc. could go here too ... somewhere?      

    $basepath = explode('/', $baseurl); // will this handle correctly when query strings have '/'
    $relpath = explode('/', $relativeurl);

    array_pop($basepath);

    $returnpath = array_merge($basepath, $relpath);
    $returnpath = array_reverse($returnpath);

    $parents = 0;
    foreach($returnpath as $part_nr => $part_value) {
        /* if we find '..', remove this and the next element */
        if($part_value == '..') {
            $parents++;
            unset($returnpath[$part_nr]);
        } /* if we find '.' remove this element */
        else if($part_value == '.') {
            unset($returnpath[$part_nr]);
        } /* if this is a normal element, and we have unhandled '..', then remove this */
        else if($parents > 0) {
            unset($returnpath[$part_nr]);
            $parents--;
        }
    }
    $returnpath = array_reverse($returnpath);
    if(count($returnpath) < $minpartsinfinal) {
        return FALSE;
    }
        return implode('/', $returnpath);
}

示例:

print url2absolute("file:///path/to/some/file.html", "another_file.php?id=5") . "<br>"; // original example
print url2absolute("file:///path/to/some/file.html", "../../../../../another_file.php?id=5") . "<br>"; // should be an error!
print url2absolute("http://path/to/some/file.html?source=this/one", "another_file.php?id=5") . "<br>"; // with query string on base URL
print url2absolute("http://path/to/some/file.html", "//other-path/another_file.php?id=5") . "<br>"; // scheme relative

答案 1 :(得分:1)

您可以使用parse_url()将URL分成几部分,然后将正向斜杠字符上的“路径”部分拆分。这应该允许你重新组装它们并替换最后一部分。

像这样(伪代码,未经测试,不确定它甚至是有效的PHP语法):

$url_parts = parse_url($url_text);
$path_parts = explode('/', $url_parts[path]);

$new_url = $url_parts[scheme] + ":";

if ($url_parts[scheme] == "file") {
    $new_url .= '///';
} else {
    $new_url .= '//';
}

$new_url .= $url_parts[hostname] . '/';
for (int i = 0; i < count($path_parts) - 1; i++) {
    $new_url .= $path_parts[i] . "/";
} 

$new_url .= $REPLACEMENT_FILENAME

如果需要,可以在末尾追加查询字符串和/或锚点片段(以#开头) - 请参阅parse_url()手册页,了解其数组中URL部分的列表。

答案 2 :(得分:1)

<?php
/* strings from your exemple */
$base_url = "file:///path/to/some/file.html";
$relative_url = "another_file.php?id=5";

/* split up urls folder parts into an array */
$base_url_parts = explode('/', $base_url);
$relative_parts = explode('/', $relative);

/* remove last element (in this case "file.html") */
array_pop($base_url_parts);

/* merge absolute_url from base and relative */
$absolute_url_parts = array_merge($base_url_parts, $relative_parts);

/* reverser the list before the search of '..' */
$absolute_url_parts = array_reverse($absolute_url_parts);

/* count of current number of unhandled '..' */
$parent_folder_count = 0;

/* loop throught all elements looking for '..' */
foreach($absolute_url_parts as $part_nr => $part_value)
{
    /* if we find '..', remove this and the next element */
    if($part_value = '..')
    {
        $parent_folder_count++;
        unset($absolute_url_parts[$part_nr]);
    }

    /* if we find '.' remove this element */
    else if($part_value = '.')
    {
        unset($absolute_url_parts[$part_nr]);
    }

    /* if this is a normal element, and we have unhandled '..', then remove this */
    else if($parent_folder_count > 0)
    {
        unset($absolute_url_parts[$part_nr]);
        $parent_folder_count--;
    }

    /* else: keep it */
}

/* restore the order by reversing again */
$absolute_url_parts = array_reverse($absolute_url_parts);

/* restore the list to a string again */
$absolute_url = implode('/', $absolute_url_parts);

/* done */
?>

答案 3 :(得分:1)

我认为最简单的解决方案是使用dirname()函数。

$url = 'file:///path/to/some/file.html';
$rel = 'another_file.php?id=5';

$final = dirname($url).'/'.$rel;

答案 4 :(得分:0)

$ab="file:///path/to/some/file.html";
$rel="another_file.php?id=5";

$exab=explode("/",$ab);
$exab[count($exab)-1]=$rel;

$newab=implode("/",$exab);

可能不是最优雅的解决方案,但它确实有效。

答案 5 :(得分:0)

$file1 = "file://path/to/some/file.html";
$file2 = "anotherfile?q=1";

$newurl = substr_replace($file1, $file2, strrpos($file1, "/")+1);

http://codepad.org/370Yp1M7