Page 1 of 1

Absolute URL from relative links

Posted: Mon Oct 21, 2002 2:47 pm
by f1nutter
Hi,

I am trying to construct a URL from links found on a page. I would like to take the address of the current page, and "append" the link appropriately. For example:

Code: Select all

$base_url = "http://www.somewhere.com/directory/page.php";

$link = "/index.htm"; // root directory file
$full_url = "http://www.somewhere.com/index.htm";

$link = "next.htm"; // file in current directory
$full_url = "http://www.somewhere.com/directory/next.htm"

$link = "#top";
$full_url = "http://www.somewhere.com/directory/page.php#top";
And this is what I have so far:

Code: Select all

<?php

$base_url = "http://www.somewhere.com/directory/index.php";
$link = "/hello.htm";

$new_url = construct_url($base_url, $link);
print($new_url);

function construct_url($base_url, $link)
{
 // if link starts http:// (escaped), it is a fully specified url, just return the link
 if (starts_with($link, "http:\/\/"))
 {
  $full_url = $link;
 }

 // if link starts with root directory (escaped regular expression)
 if (starts_with($link, "\/"))
 {
  // Split URL into components
  $base_url = parse_url($base_url);

  $full_url = $base_urlї"scheme"] . "://" . $base_urlї"host"] . $link;
 }

 // if link starts #, it is an anchor and can be appended
 if (starts_with($link, "\#"))
 {
  $full_url = $base_url . $link;
 }

 // if link starts with query, append to file
 if (starts_with($link, "\?"))
 {
  // Split URL into components
  $base_url = parse_url($base_url);

  $full_url = $base_urlї"scheme"] . "://" . $base_urlї"host"] . $base_urlї"path"] . $link;
 }

 return $full_url;
}

function starts_with($string, $regex)
{
 $pattern = "/(^" .$regex. ")/";

 return preg_match($pattern, $string);
}

function ends_with($string, $regex)
{
 $pattern = "/(" .$regex. "$)/";

 return preg_match($pattern, $string);
}

?>
For those of you familiar with Java, I need something exactly like

Code: Select all

URL(URL urlObj, String urlSpecifier)
This takes a URL object and a string and returns the new full URL.

Web pages being the wonderfully diverse work of art they are throw up a few surprises. How do I handle the following:
  • links begining ./
    links begining ../
    base url ending /
Bear in mind all the ways you could write a valid link, and you know what I am up against. (Even the Java version is not perfect!)

Cheers.