Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform relative path into absolute URL using PHP

Tags:

How to, using php, transform relative path to absolute URL?

like image 577
Paul Avatar asked Dec 14 '10 21:12

Paul


People also ask

What is absolute URL relative URL?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.

What is absolute and relative path in PHP?

An absolute path refers to a file on the Internet using its full URL, e.g. "http://www.uvsc.edu/disted/php/webct/itr/index.php" A relative path assumes that the file is on the current server, e.g. "php/webct/itr/index. php".

Is absolute URL better than relative?

An absolute URL contains more information than a relative URL does. Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.


2 Answers

function rel2abs($rel, $base) {     /* return if already absolute URL */     if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;      /* queries and anchors */     if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;      /* parse base URL and convert to local variables:        $scheme, $host, $path */     extract(parse_url($base));      /* remove non-directory element from path */     $path = preg_replace('#/[^/]*$#', '', $path);      /* destroy path if relative url points to root */     if ($rel[0] == '/') $path = '';      /* dirty absolute URL */     $abs = "$host$path/$rel";      /* replace '//' or '/./' or '/foo/../' with '/' */     $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');     for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}      /* absolute URL is ready! */     return $scheme.'://'.$abs; } 
like image 183
bimbom22 Avatar answered Oct 26 '22 22:10

bimbom22


I love the code that jordanstephens provided from the link! I voted it up. l0oky inspired me to make sure that the function is port, username, and password URL compatible. I needed it for my project.

function rel2abs( $rel, $base ) {     /* return if already absolute URL */     if( parse_url($rel, PHP_URL_SCHEME) != '' )         return( $rel );      /* queries and anchors */     if( $rel[0]=='#' || $rel[0]=='?' )         return( $base.$rel );      /* parse base URL and convert to local variables:        $scheme, $host, $path */     extract( parse_url($base) );      /* remove non-directory element from path */     $path = preg_replace( '#/[^/]*$#', '', $path );      /* destroy path if relative url points to root */     if( $rel[0] == '/' )         $path = '';      /* dirty absolute URL */     $abs = '';      /* do we have a user in our URL? */     if( isset($user) )     {         $abs.= $user;          /* password too? */         if( isset($pass) )             $abs.= ':'.$pass;          $abs.= '@';     }      $abs.= $host;      /* did somebody sneak in a port? */     if( isset($port) )         $abs.= ':'.$port;      $abs.=$path.'/'.$rel;      /* replace '//' or '/./' or '/foo/../' with '/' */     $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');     for( $n=1; $n>0; $abs=preg_replace( $re, '/', $abs, -1, $n ) ) {}      /* absolute URL is ready! */     return( $scheme.'://'.$abs ); } 
like image 39
Mikey A. Leonetti Avatar answered Oct 27 '22 00:10

Mikey A. Leonetti