[英]Relative + base URL to absolute URL?
基本上,给定基本网址,例如
file:///path/to/some/file.html
以及相对网址
another_file.php?id=5
我要出去
file:///path/to/some/another_file.php?id=5
我发现这个脚本 (这是一样的这一个 ),但它似乎并没有在工作file://
方案。 在使用我的代码之前,我正在做一些本地测试,所以我想同时使用file://
和http://
。
有人知道将执行此操作的脚本/功能吗?
在C#中,我将使用Uri(Uri base,string rel) 。
以上仅是示例。 它可以在您可以放入<a href="xxx">
任何 URL上<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;
}
您可以使用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
如果需要,您可以在末尾附加查询字符串和/或锚定片段(以#开头)-有关该数组中URL部分的列表,请参见parse_url()手册页。
我已经修改了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
<?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 */
?>
我认为最简单的解决方案是使用dirname()函数。
$url = 'file:///path/to/some/file.html';
$rel = 'another_file.php?id=5';
$final = dirname($url).'/'.$rel;
$ab="file:///path/to/some/file.html";
$rel="another_file.php?id=5";
$exab=explode("/",$ab);
$exab[count($exab)-1]=$rel;
$newab=implode("/",$exab);
可能不是最优雅的解决方案,但它可行。
$file1 = "file://path/to/some/file.html";
$file2 = "anotherfile?q=1";
$newurl = substr_replace($file1, $file2, strrpos($file1, "/")+1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.