0

I have a simple strange problem but I can not find a function to do this after many search.

I have an URL like http://example.com/folder/folder2/../image/test.jpg and I would like a function which return the correct absolute link:

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

A function with only one param, the url (and not base dir or relative dir like in examples I found)

If you can help me, thanks.

2 Answers 2

1

Perhaps a starting point:

<?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');

Output:

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

You may want to see how browsers and other web clients de-relativify (urls).

Sign up to request clarification or add additional context in comments.

1 Comment

Please try and break this.
0

thanks to everyone for your answers.

here are a recap and some other ways i've tested

            <?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);
            ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.