将远程相对路径转换为绝对路径

将远程相对路径转换为绝对路径

问题描述:

我尝试寻找类似的问题,但未能成功.

I tried looking for a similar question, but was unable to.

我正在寻找正确的方向.我目前正在做的是 现在可以收集远程站点的所有href值的列表,因为其中一些可以 相对路径,我需要一个构建绝对路径的函数.

I'm looking for push in the right direction. What I'm currently doing is gathering a list of all href values of a remote site, now since some of them can be relative paths, I need a function that builds an absolute path.

由于我拥有域名(通过遵循使用的最后一个URL cUrl):

Since I have the domain name (by following the last url cUrl used):

$base_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

现在,假设$ base_url的值为: http://www.example.com/home/index.html 我当前正在读取的href值为:/styles/ie.css

Now lets say $base_url value is: http://www.example.com/home/index.html and href value I'm currently reading is: /styles/ie.css

我需要将$ base_url的值更改为 http://www.example.com/样式/ie.css 但是我需要该功能尽可能地动态.看看 可能的情况(可能不是全部):

I need to turn the value of $base_url to http://www.example.com/styles/ie.css but I need that function to be dynamic as much as possible. Take a look at the possible scenarios (probably a not all):

1 = base url
2 = relative path
------------------------------------------------
1 http://www.example.com/
2 java/popups.js

1 + 2 = http://www.example.com/java/popups.js
------------------------------------------------
1 http://www.example.com
2 java/popups.js

1 + / + 2 = http://www.example.com/java/popups.js
------------------------------------------------
1 http://www.example.com/mysite/
2 ../java/popups.js 

1 - / + (2 - ..) = http://www.example.com/java/popups.js
------------------------------------------------

1 http://www.example.com/rsc/css/intlhplib-min.css
2 ../images/sunflower.png

1 - /css/intlhplib-min.css + (2 - ..) = http://www.example.com/rsc/images/sunflower.png     

在@bozdoz向正确的方向推动后,我最终编写了自己的函数.

I ended up writing my own function, after a push in the right direction from @bozdoz.

该函数有两个参数,第一个是$ resource,这是相对文件路径. 第二个是基本网址(将用于构造绝对网址).

The function takes two arguments, first one is $resource, which is the relative file path. And the second one is is the base url (which will be used to construct an absolute url).

这是出于我的项目目的而设计的,我不确定它是否适合正在寻找的人 对于类似的解决方案.随时使用它,并提供任何效率方面的改进.

This was design for my project purposes, I'm not sure it will fit anyone who is looking for a similar solution. Feel free to use it, and provide any efficiency improvements.

更新版本:感谢 Tim Cooper

function rel2abs_v2($resource, $base_url) 
{
$base_url = parse_url($base_url);

if(substr($resource, 0, 4) !== "http" && substr($resource, 0, 5) !== "https") // if no http/https is present, then {$resource} is a relative path.
{
# There is a "../" in the string
if (strpos($resource, "../") !== false)
{
$dir_count = substr_count($resource, "../");

$path_array = explode("/", $base_url["path"]);
$path_count = count($path_array); // 4
$path_index = ($path_count - $dir_count) - 2;

$resource = trim(str_replace("../", "", $resource));

if($path_index > 0) { $fs = "/"; }

if($dir_count > 0)
{
$base_url_path = implode("/", array_slice($path_array, $dir_count, $path_index - $dir_count + 1));
return $base_url['scheme'] . '://' . $base_url['host'] . $fs . $base_url_path ."/". $resource;
}
}

# Latest addition - remove if unexplained behaviour is in place.
if(starts_with($resource, "//"))
{
return trim(str_replace("//", "", $resource));      
}

if (starts_with($resource, "/"))
{
return $base_url["scheme"] . "://" . $base_url["host"] . $resource;
}
else
{
$path_array = explode("/", $base_url["path"]);

end($path_array);
$last_id = key($path_array);

return $base_url["scheme"] . "://" . $base_url["host"] . "/" . $path_array[--$last_id] . "/" . $resource;
}

}
else
{
return $resource;
}
}