Converting relative to absolute links in PHP

Skittled

New member
Oct 16, 2007
84
0
0
Okay. I'm scraping some stuff in php; sometimes links are in a relative format (e.g. "/logout") rather than absolute (http://example.com/account/logout). There's a PECL extension that merges these and gives you an absolute address, but I need something a little more lightweight. i.e. just the function in php.

Any hints on resolving absolute uris from relative?
 


Gah...10 minute edit rule.

Forgot to say it's the "../../index.html"'s that're fucking me up. Without them it's easy to resolve.
 
Search for anchor tags and edit href:

$href_in_quotes = str_replace('/../../', '', $href_in_quotes);

$new_href = 'http://' . $_SERVER['HTTP_HOST'] .$href_in_quotes;
 
Search for anchor tags and edit href:

$href_in_quotes = str_replace('/../../', '', $href_in_quotes);

$new_href = 'http://' . $_SERVER['HTTP_HOST'] .$href_in_quotes;

Nope, because that's just replacing the references, not actually processing them. Sure, it'll work IF it starts with / and has two sets of ..'s but all other situations will fail. e.g. I want to take a base of "example.com/account/skittled/", and a link of "../index.html", and get "example.com/account/index.html".

I'm pretty close to a working function now. Will post when done.