PHP:如何从HTML页面获取基本URL

PHP:如何从HTML页面获取基本URL,php,url,base,Php,Url,Base,我正在努力想办法做到这一点。我有一个HTML页面的绝对URL,我需要得到这个页面的基本URL。因此URL可以是例如: 等等。因此,第一个问题是从这些URL和其他URL中查找基本URL。第二个问题是一些HTML页面包含一个基本标记,例如http://example.com/或者干脆/(尽管我认为有些浏览器只支持以协议开始的浏览器://?) 不管怎样,我怎样才能正确地在PHP中实现这一点?我有URL,我有加载在DOMDocument中的HTML,所以应该能够相当容易地获取基本标记(如果它

我正在努力想办法做到这一点。我有一个HTML页面的绝对URL,我需要得到这个页面的基本URL。因此URL可以是例如:

等等。因此,第一个问题是从这些URL和其他URL中查找基本URL。第二个问题是一些HTML页面包含一个基本标记,例如
http://example.com/
或者干脆
/
(尽管我认为有些浏览器只支持以
协议开始的浏览器://
?)

不管怎样,我怎样才能正确地在PHP中实现这一点?我有URL,我有加载在DOMDocument中的HTML,所以应该能够相当容易地获取基本标记(如果它存在的话)。例如,浏览器如何解决这个问题


澄清我为什么需要这个

我正在尝试创建一个网页的URL,并返回该网页链接到的所有图像的绝对URL。由于这些图像中的一些/许多/所有可能都有相对URL,因此我需要找到基本URL,以便在将它们设置为绝对URL时使用。这可能是网页的基本URL,也可能是HTML本身中指定的基本URL

我已经设法获取HTML并找到URL。我想我还找到了一种工作方法,当我有基本URL要使用时,使URL绝对化。但是,查找基本URL是我所缺少的,也是我在这里要问的问题。

请参阅

从中选择你要寻找的元素。您可能需要
$result['path']
请参阅

从中选择你要寻找的元素。您可能想要
$result['path']

代码片段的乐趣

if (!function_exists('base_url')) {
    function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
        if (isset($_SERVER['HTTP_HOST'])) {
            $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
            $hostname = $_SERVER['HTTP_HOST'];
            $dir =  str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

            $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
            $core = $core[0];

            $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
            $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
            $base_url = sprintf( $tmplt, $http, $hostname, $end );
        }
        else $base_url = 'http://localhost/';

        if ($parse) {
            $base_url = parse_url($base_url);
            if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
        }

        return $base_url;
    }
}
使用简单到:

//  url like: http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php

echo base_url();    //  will produce something like: http://stackoverflow.com/questions/2820723/
echo base_url(TRUE);    //  will produce something like: http://stackoverflow.com/
echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE);    //  will produce something like: http://stackoverflow.com/questions/
//  and finally
echo base_url(NULL, NULL, TRUE);
//  will produce something like: 
//      array(3) {
//          ["scheme"]=>
//          string(4) "http"
//          ["host"]=>
//          string(12) "stackoverflow.com"
//          ["path"]=>
//          string(35) "/questions/2820723/"
//      }
有趣的片段

if (!function_exists('base_url')) {
    function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
        if (isset($_SERVER['HTTP_HOST'])) {
            $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
            $hostname = $_SERVER['HTTP_HOST'];
            $dir =  str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

            $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
            $core = $core[0];

            $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
            $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
            $base_url = sprintf( $tmplt, $http, $hostname, $end );
        }
        else $base_url = 'http://localhost/';

        if ($parse) {
            $base_url = parse_url($base_url);
            if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
        }

        return $base_url;
    }
}
使用简单到:

//  url like: http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php

echo base_url();    //  will produce something like: http://stackoverflow.com/questions/2820723/
echo base_url(TRUE);    //  will produce something like: http://stackoverflow.com/
echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE);    //  will produce something like: http://stackoverflow.com/questions/
//  and finally
echo base_url(NULL, NULL, TRUE);
//  will produce something like: 
//      array(3) {
//          ["scheme"]=>
//          string(4) "http"
//          ["host"]=>
//          string(12) "stackoverflow.com"
//          ["path"]=>
//          string(35) "/questions/2820723/"
//      }

问题是路径只给出了最后一部分,这意味着我必须从所有的片段重新构建整个url。我希望可能有类似于
deparse\u url
之类的东西。另外,我不确定parse\u url是否删除了“page.html”部分?这不是路的一部分吗?我真的不明白你在问什么。你能举个例子吗?是的,page.html是路径的一部分。为问题添加了一些说明。希望这能让事情变得更清楚。如果没有,请告诉我!问题是路径只给出了最后一部分,这意味着我必须从所有的片段重新构建整个url。我希望可能有类似于
deparse\u url
之类的东西。另外,我不确定parse\u url是否删除了“page.html”部分?这不是路的一部分吗?我真的不明白你在问什么。你能举个例子吗?是的,page.html是路径的一部分。为问题添加了一些说明。希望这能让事情变得更清楚。如果没有,请告诉我!