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

Php 将远程相对路径转换为绝对路径,php,html,parsing,Php,Html,Parsing,我试图寻找一个类似的问题,但没有成功 我在寻找正确的方向。我现在正在做的是 正在收集远程站点的所有href值的列表,因为其中一些值可以 相对路径,我需要一个构建绝对路径的函数 因为我有域名(通过使用最后一个url): 现在让我们假设$base_url值为: 我目前读到的href值是:/style/ie.css 我需要将$base\u url的值转换为 但我需要这个函数尽可能地动态。看一看 可能的情况(可能不是全部): 我认为您需要在href路径上使用正则表达式,以确保其一致性。您还可以从()获得

我试图寻找一个类似的问题,但没有成功

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

因为我有域名(通过使用最后一个url):

现在让我们假设$base_url值为: 我目前读到的href值是:/style/ie.css

我需要将$base\u url的值转换为 但我需要这个函数尽可能地动态。看一看 可能的情况(可能不是全部):


我认为您需要在href路径上使用正则表达式,以确保其一致性。您还可以从()获得准确的基本url:

并将其添加到输出变量:

$abspath = $url['scheme'] . '://' . $url['host'] . '/' . $first_directory . $href;
另一个更新 要查找href值与基本url的关系,可以搜索href值开头出现的
。/
/
,然后相应地调整绝对url。这将有助于您了解以下情况:

<?php
$href = '../../images/sunflower.png';
preg_match('~^(\.{0,2}\/)+~', $href, $matches); //preg_match to check if it exists
if (substr_count($matches[0], '../')){ // substr_count to count number of '../'
    echo 'Go up ' . substr_count($matches[0], '../') . ' directories';
}
else if (substr_count($matches[0], '/')){
    echo 'Root directory';
}
else {
    echo 'Current directory';
}
?>


查看上的演示。

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

该函数有两个参数,第一个参数是$resource,它是相对文件路径。 第二个是基本url(用于构造绝对url)

这是为我的项目设计的,我不确定它是否适合任何正在寻找的人 对于类似的解决方案。您可以随意使用它,并提供任何效率改进

更新版本感谢


你从哪里得到href值?我得到整个html,然后解析它。我最终得到了一个从任何href属性中检索到的值数组。我的答案是否解决了您的问题@elad.chen?@bozdoz我正在测试它,我们会告诉您的。这不会涵盖大多数可能的结果。如果href值为images/sunflower.png,并且url是绝对路径,那么您总是希望url变量中的第一个目录?你的问题没有说清楚。嗯,不完全是这样。我需要一种方法来计算各种场景中的相对路径。我可以看到url[“path”]包含目录结构,但我在计算所有可能结果的方法时遇到了问题。如果href值开头没有“/”字符,则表示它相对于当前工作目录,但如果有“/”字符,则表示它应该向上移动到一个目录。这同样适用于以后,当我尝试解析css文件可以放在“home/resources/styles.css”下的相对css图像路径时,它将有url(“../../images”)。@elad.chen我明白你的意思了。您必须在href上进行正则表达式匹配和计数,检查
\.{0,2}\/
是否可以提供一个示例?
<?php
$href = '../images/sunflower.png';
$href = preg_replace('~^\.{0,2}\/~', '', $href);
?>
<?php
$url = 'http://www.example.com/home/index.html';
$url = parse_url($url);

$abspath = $url['scheme'] . '://' . $url['host'] . '/' . $href;

echo $abspath;
?>
$first_directory = '';
if (isset($url['path'])) {
    $patharray = explode('/', $url['path']);
    if (count($patharray)>2){
        $first_directory = explode('/', $url['path'])[1] . '/';
    }
}
$abspath = $url['scheme'] . '://' . $url['host'] . '/' . $first_directory . $href;
<?php
$href = '../../images/sunflower.png';
preg_match('~^(\.{0,2}\/)+~', $href, $matches); //preg_match to check if it exists
if (substr_count($matches[0], '../')){ // substr_count to count number of '../'
    echo 'Go up ' . substr_count($matches[0], '../') . ' directories';
}
else if (substr_count($matches[0], '/')){
    echo 'Root directory';
}
else {
    echo 'Current directory';
}
?>
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;
}
}