PHP - Function to remove character and concatenate strings (relative URL to absolute)

Asked

Viewed 369 times

0

Good staff!

I have a small problem (problem because I don’t work much with php).

I’m using a script attached to phpbb that displays the topics of a given forum on an external page, but cms returns me only the relative url (the script is in the root of the site and phpbb installed in ./forum), so I can’t create an rss for example, that requires me the absolute url.

The format returned by phpbb is ./forum/viewtopic.php?f=xx&t=xx

What I need is not something so deep in phpbb, but simply a way of translating this relative path into an absolute path, simply removing that first point and adding for example http://exemplo.com in place, then staying http://exemplo.com/forum/viewtopic.php?f=xx&t=xx.

How could I do a function to work this using php (maybe a function that uses regex, I don’t know)?

Thank you in advance!

  • Dude, relative URL never has "." (dots). If it is returned "dot" it is because it is physical path, not URL.

  • Moreover, this depends a lot. You want from a given script (rss.php, for example) to get the absolute URL of the current running script?

  • Not exactly, felipsmartins. The dot character is used to assign a current directory or indentation directory in the case of two dots ../. It can be used in filesystem or virtual physical path (url). It has nothing to do with identifying whether it is relative or absolute. A relative or absolute path can contain indentations. In short, the right way to say is normalize url or "normalize a path". A subtle but significant difference.

2 answers

0


Well, as commented above, the path I get is physical, and I need to turn to an absolute URL.

Anyway, an international stack user answered me:

$url = str_replace('./', 'http://example.com/', $url);

  • . / does not represent the root directory.

0

The appropriate term is canonize or normalize url

Routine example to normalize a url:

function canonicalize_url($str)
{

    $protocol = '';

    if (strpos($str, 'http://') !== false) {
        $protocol = 'http://';
        $str = substr($str, 7);
    } else if (strpos($str, 'https://') !== false) {
        $protocol = 'https://';
        $str = substr($str, 8);     
    } else if (strpos($str, '//') === 0) {
        $protocol = '//';
        $str = substr($str, 2);     
    }

    if (strpos($str, '/..') !== false) {
        $str = explode('/', $str);
        $first = $str[0];
        $keys = array_keys($str, '..');

        foreach ($keys as $k => $v) {
            array_splice($str, $v - ($k * 2 + 1), 2);
        }

        if (empty($str)) {
            $str = array($first);
        } else {
            reset($str);
            if ($first != $str[0]) {
                array_unshift($str, $first);
            }
        }

        return $protocol.str_replace('./', '', implode('/', $str));
    }

    return $protocol.str_replace('./', '', $str);

}

// testando url absoluta (http, https e protocolo genérico //)
$url = 'https://www.foo.bar/folder1/../folder2/bar/../../..';
$url = 'http://www.foo.bar/folder1/../folder2/bar/../';
$url = '//www.example.com/folder1/../folder2/bar/../../..';

// testando exceço de recuos. A função detecta e impede o recuo além da raiz da url (virtualmente).
$url = '//www.example.com/folder1/../folder2/bar/../../../../../../../../';

// testando url relativa
$url = './folder1/folder2/';
echo canonicalize_url($url); //http://www.example.com/else

The above function does not identify duplicate bars, for example http://www.foo.bar/..////pasta

Browser other questions tagged

You are not signed in. Login or sign up in order to post.