开发者

Convert URI to URL

How to convert an URI to URL if I know the current site pa开发者_开发技巧th?

Consider these examples:

Current path is: `http://www.site.com/aa/folder/page1.php

Uri: folder2/page.php

Uri: /folder2/page.php


And what if the current path is:

`http://www.site.com/aa/folder/

or

`http://www.site.com/aa/folder

What the URLs will look like then?

I know this should be easy and obvious, but I can't find anywhere the complete answer (and yes, I did searched on Google)


Here is a block of code that has the function that you need: http://ca.php.net/manual/en/function.parse-url.php#76682

Edit: The above linked function modified with an example

<?php

var_dump(resolve_url('http://www.site.com/aa/folder/page1.php','folder2/page.php?x=y&z=a'));

var_dump(resolve_url('http://www.site.com/aa/folder/page1.php','/folder2/page2.php'));

function unparse_url($components) {
    return $components['scheme'].'://'.$components['host'].$components['path'];
}

/**
 * Resolve a URL relative to a base path. This happens to work with POSIX
 * filenames as well. This is based on RFC 2396 section 5.2.
 */
function resolve_url($base, $url) {
        if (!strlen($base)) return $url;
        // Step 2
        if (!strlen($url)) return $base;
        // Step 3
        if (preg_match('!^[a-z]+:!i', $url)) return $url;
        $base = parse_url($base);
        if ($url{0} == "#") {
                // Step 2 (fragment)
                $base['fragment'] = substr($url, 1);
                return unparse_url($base);
        }
        unset($base['fragment']);
        unset($base['query']);
        if (substr($url, 0, 2) == "//") {
                // Step 4
                return unparse_url(array(
                        'scheme'=>$base['scheme'],
                        'path'=>$url,
                ));
        } else if ($url{0} == "/") {
                // Step 5
                $base['path'] = $url;
        } else {
                // Step 6
                $path = explode('/', $base['path']);
                $url_path = explode('/', $url);
                // Step 6a: drop file from base
                array_pop($path);
                // Step 6b, 6c, 6e: append url while removing "." and ".." from
                // the directory portion
                $end = array_pop($url_path);
                foreach ($url_path as $segment) {
                        if ($segment == '.') {
                                // skip
                        } else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
                                array_pop($path);
                        } else {
                                $path[] = $segment;
                        }
                }
                // Step 6d, 6f: remove "." and ".." from file portion
                if ($end == '.') {
                        $path[] = '';
                } else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
                        $path[sizeof($path)-1] = '';
                } else {
                        $path[] = $end;
                }
                // Step 6h
                $base['path'] = join('/', $path);

        }
        // Step 7
        return unparse_url($base);
}

?>


The $_SERVER superglobal will have the information you're looking for, namely $_SERVER['REQUEST_URI'] and $_SERVER['SERVER_NAME']. $_SERVER['QUERY_STRING'] might also be useful.

Please see:

http://php.net/manual/en/reserved.variables.server.php


php has pathinfo(), realpath() and parseurl() and other filesystem and url path functions. Used together with info from the $_SERVER superglobal (as mentioned by andre), you should be able to do what you need.


$uri = "http://www.site.com/aa/folder/";

$url = explode("/", $uri);
$url = $url[2];

echo $url; //www.site.com

Is this what you are looking for?


If you install PECL pecl_http, you can make use of http_build_url:

http_build_url("http://www.site.com/aa/folder/page1.php",
                 array("path" => "folder2/page.php"));

and you pass any of your relative URI(L)s as path. The function will make sure to build the correct one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜