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";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);
}
?>Code: Select all
URL(URL urlObj, String urlSpecifier)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 /
Cheers.