How to parse relative links to absolute links?

They have: 60 posts

Joined: Aug 2000

I know we have to use reg. expression, but any suggestions would be very helpful!
for example: -->

(that is just a SIMPLE form of link, take other formats of hyperlink into consideration too)

http://new.123finder.com/ - Helps you find cool domains for FREE & Registers it for only $14.95/yr
http://www.ad-rotator.com/ - Free ad rotating system for small websites - Stop Ctrl-P/Ctrl-C the ad code!

------
Son

Ken Elliott's picture

They have: 358 posts

Joined: Jun 1999

Here is a some regular expressions that will do it. Just substitute $url with the url to be used and $file to the file to be searched. It's not tested so check for errors.

#!/usr/bin/perl -w #change to yer perl root

$url = "href=\"http://www.bleh.com/";
#you have to have href=" in it to replace the match
$file = "file.html";

open (FILE, < "$file");
@contents = ;
close (FILE);

foreach $line(@contents) {
$line =~ s/href="/$url/;
}

This will replace every occurance of - href=" - so if you have some that you don't want to be changed...banners etc. You will need to change them back.

VulKen
What the world needs, is more tutorials!

Pimpin like a pimp with an electrofied pimpin machine!

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

that won't work for stuff in subdirectories, like:
http://blah.com/my_dir/

not tested:

$BaseURL = "http://"."$ENV{HTTP_HOST}"."$ENV{REQUEST_URI}";
$BaseURL =~ /(.*)\/(.*).(.*)/;
$BaseURL = "http=\"$1/";

open(FILE, "your.html");
$ThePage = join("|break|", <FILE>);
close(FILE);

$ThePage =~ s/http="/$BaseURL/g;
$ThePage =~ s/\|break\|/\n/g;
'

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.