php转换绝对URL包含绝对URL中的相对路径而没有相对路径

时间:2018-04-25 10:24:10

标签: php url path

我有一个简单的奇怪问题,但经过多次搜索后我无法找到执行此操作的功能。

我有一个像http://example.com/folder/folder2/../image/test.jpg这样的网址,我想要一个返回正确绝对链接的函数:

http://example.com/folder/image/test.jpg

只有一个param的函数,url(而不是我发现的示例中的基础目录或相对目录)

如果你可以帮助我,谢谢。

2 个答案:

答案 0 :(得分:1)

也许是一个起点:

<?php
function unrelatify($url)
{
    $parts     = parse_url($url);
    $path      = $parts['path'] ?? '';
    $hierarchy = explode('/', $path);

    while(($key = array_search('..', $hierarchy)) !== false) {
        if($key-1 > 0)
            unset($hierarchy[$key-1]);
        unset($hierarchy[$key]);
        $hierarchy = array_values($hierarchy);
    }
    $new_path = implode('/', $hierarchy);

    return str_replace($path, $new_path, $url);
}

echo unrelatify('http://example.com/../folder/../folder2/../image/test.jpg#foo?bar=baz');

输出:

http://example.com/image/test.jpg#foo?bar=baz

您可能想看看浏览器和其他网络客户端如何去相关化(网址)。

答案 1 :(得分:0)

感谢大家的回答。

这里是一个回顾和其他一些我测试过的方法

            <?php

            function unrelatify($url) {
                $parts = parse_url($url);
                $path = $parts['path'];
                $hierarchy = explode('/', $path);

                while (($key = array_search('..', $hierarchy)) !== false) {
                    if ($key - 1 > 0)
                        unset($hierarchy[$key - 1]);
                    unset($hierarchy[$key]);
                    $hierarchy = array_values($hierarchy);
                }
                $new_path = implode('/', $hierarchy);

                return str_replace($path, $new_path, $url);
            }

            function normalizePath($path) {
                do {
                    $path = preg_replace(
                            array('#//|/\./#', '#/([^/.]+)/\.\./#'), '/', $path, -1, $count
                    );
                } while ($count > 0);
                return str_replace('../', '', $path);
            }

            function processUrl($url) {
                $parsedUrl = parse_url($url);
                $path = $parsedUrl['path'];
                $pathSegments = explode("/", $path);
                $iterator = 0;
                $removedElements = 0;
                foreach ($pathSegments as $segment) {
                    if ($segment == "..") {
                        if ($iterator - $removedElements - 1 < 0) {
                            return false;
                        }
                        unset($pathSegments[$iterator - $removedElements - 1]);
                        unset($pathSegments[$iterator]);
                        $removedElements += 2;
                    }
                    $iterator++;
                }

                $parsedUrl['path'] = implode("/", $pathSegments);

                $newUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . "/" . $parsedUrl['path'];

                return $newUrl;
            }

            function path_normalize($path) {
                $path = str_replace('\\', '/', $path);
                $blocks = preg_split('#/#', $path, null, PREG_SPLIT_NO_EMPTY);
                $res = array();

                while (list($k, $block) = each($blocks)) {
                    switch ($block) {
                        case '.':
                            if ($k == 0)
                                $res = explode('/', path_normalize(getcwd()));
                            break;
                        case '..';
                            if (!$res)
                                return false;
                            array_pop($res);
                            break;
                        default:
                            $res[] = $block;
                            break;
                    }
                }
                $r = implode('/', $res);

                return $r;
            }

            echo 'path_normalize<br />';
            $url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . path_normalize($url);


            echo '<hr />';
            $url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . path_normalize($url);


            echo '<hr />normalizePath<br />';
            $url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . normalizePath($url);


            echo '<hr />';
            $url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . normalizePath($url);


            echo '<hr />unrelatify<br />';
            $url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . unrelatify($url);


            echo '<hr />';
            $url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . unrelatify($url);


            echo '<hr />processUrl<br />';
            $url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . processUrl($url);


            echo '<hr />';
            $url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
            echo $url . ' === > ' . processUrl($url);
            ?>