I have coded a function which converts relative URL to absolute URL for a project of mine. Considering I could not find it elsewhere, I figured I would post it here.
The following function takes in 2 parameters, the first parameter is the URL you want to convert from relative to absolute, and the second parameter is a sample of the absolute URL.
Currently it does not resolve '../' in the URL, only because I do not need it. Most webservers will resolve this for you. If you want it to resolve the '../' in the path, it just takes minor modifications.
<?php
function relativeToAbsolute($inurl, $absolute) {
// Get all parts so not getting them multiple times :)
$absolute_parts = parse_url($absolute);
// Test if URL is already absolute (contains host, or begins with '/')
if ( (strpos($inurl, $absolute_parts['host']) == false) ) {
// Define $tmpurlprefix to prevent errors below
$tmpurlprefix = "";
// Formulate URL prefix (SCHEME)
if (!(empty($absolute_parts['scheme']))) {
// Add scheme to tmpurlprefix
$tmpurlprefix .= $absolute_parts['scheme'] . "://";
}
// Formulate URL prefix (USER, PASS)
if ((!(empty($absolute_parts['user']))) and (!(empty($absolute_parts['pass'])))) {
// Add user:port to tmpurlprefix
$tmpurlprefix .= $absolute_parts['user'] . ":" . $absolute_parts['pass'] . "@";
}
// Formulate URL prefix (HOST, PORT)
if (!(empty($absolute_parts['host']))) {
// Add host to tmpurlprefix
$tmpurlprefix .= $absolute_parts['host'];
// Check for a port, add if exists
if (!(empty($absolute_parts['port']))) {
// Add port to tmpurlprefix
$tmpurlprefix .= ":" . $absolute_parts['port'];
}
}
// Formulate URL prefix (PATH) and only add it if the path to image does not include ./
if ( (!(empty($absolute_parts['path']))) and (substr($inurl, 0, 1) != '/') ) {
// Get path parts
$path_parts = pathinfo($absolute_parts['path']);
// Add path to tmpurlprefix
$tmpurlprefix .= $path_parts['dirname'];
$tmpurlprefix .= "/";
}
else {
$tmpurlprefix .= "/";
}
// Lets remove the '/'
if (substr($inurl, 0, 1) == '/') { $inurl = substr($inurl, 1); }
// Lets remove the './'
if (substr($inurl, 0, 2) == './') { $inurl = substr($inurl, 2); }
return $tmpurlprefix . $inurl;
}
else {
// Path is already absolute. Return it :)
return $inurl;
}
}
// Define a sample absolute URL
$absolute = "http://" . "user:[email protected]:8080/path/to/index.html"; // Just evading php.net spam filter, not sure how example.com is spam...
/* EXAMPLE 1 */
echo relativeToAbsolute($absolute, $absolute) . "\n";
/* EXAMPLE 2 */
echo relativeToAbsolute("img.gif", $absolute) . "\n";
/* EXAMPLE 3 */
echo relativeToAbsolute("/img.gif", $absolute) . "\n";
/* EXAMPLE 4 */
echo relativeToAbsolute("./img.gif", $absolute) . "\n";
/* EXAMPLE 5 */
echo relativeToAbsolute("../img.gif", $absolute) . "\n";
/* EXAMPLE 6 */
echo relativeToAbsolute("images/img.gif", $absolute) . "\n";
/* EXAMPLE 7 */
echo relativeToAbsolute("/images/img.gif", $absolute) . "\n";
/* EXAMPLE 8 */
echo relativeToAbsolute("./images/img.gif", $absolute) . "\n";
/* EXAMPLE 9 */
echo relativeToAbsolute("../images/img.gif", $absolute) . "\n";
?>
OUTPUTS:
http :// user:[email protected]:8080/path/to/index.html
http :// user:[email protected]:8080/path/to/img.gif
http :// user:[email protected]:8080/img.gif
http :// user:[email protected]:8080/path/to/img.gif
http :// user:[email protected]:8080/path/to/../img.gif
http :// user:[email protected]:8080/path/to/images/img.gif
http :// user:[email protected]:8080/images/img.gif
http :// user:[email protected]:8080/path/to/images/img.gif
http :// user:[email protected]:8080/path/to/../images/img.gif
Sorry if the above code is not your style, or if you see it as "messy" or you think there is a better way to do it. I removed as much of the white space as possible.
Improvements are welcome :)