Convert relative to absolute URL given a base URL, without modules
Aristotle
created: 2006-01-17 01:07:54

I wrote this mainly because someone asked how to do this in PHP. Turns out there is nothing in like the venerable [cpan://URI] module in all of PHP-dom (how pathetic is that for a language as web-centric as it?).

First, I tried writing this with mostly string functions, but that was too painful, so I switched to doing it using regexes, even though they’re piss-poorly supported. After a while, my eyes glazed over, so I resorted to writing and debugging the code in Perl first, and then compiling it down to machine code PHP.

I thought I’d post this byproduct here for posterity, in case someone needs it in some circumstance.

sub abs_url {
	my ( $relative, $base ) = @_;

	return $relative if $relative =~ m{ \A http:// }x;

	my ( $host, $hostrelative_abs ) = $base =~ m{
		\A
		http://         # skip scheme
		([^/]*) /?      # capture hostname
		(.*?)           # capture everything that follows, but
		(?: / [^/]+ )?  # leave out the optional final non-directory component
		\z
	}x;

	my $abs_url = join '/', $host, $hostrelative_abs, $relative;

	# replace '//' or '/./' with '/'
	1 while $abs_url =~ s! / \.? (?=/|\z) !!x;

	# remove '/foo/..' (but be careful to skip '/../..')
	1 while $abs_url =~ s! / (?!\.\.) [^/]+ / \.\. (?=/|\z) !!x;

	return "http://$abs_url";
}

perlmonks.org content © perlmonks.org and Aristotle

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03