Converting relative URL to absolute

前端 未结 2 1214
野趣味
野趣味 2021-01-19 06:26

Let\'s say I have a URL of the document linking to another document (which can be both absolute or relative) and I need to have this link in absolute.

I made the sim

2条回答
  •  礼貌的吻别
    2021-01-19 06:46

    $uri = "..";
    $path = realpath($uri);
    $root = realpath($_SERVER["DOCUMENT_ROOT"]);
    
    if($path){
        $path = str_replace($root, "", $path);
        $path = $_SERVER["SERVER_NAME"] . $path;
        $protocol = "http";
        if(isset($_SERVER["HTTPS"])){
            $protocol .= "s";        
        }
        $path = "{$protocol}://$path";
        $path = str_replace("\\", "/", $path);
    }
    
    var_dump($path);
    

    There is probably a better/quicker way, but I just knocked this up...

提交回复
热议问题