Click to See Complete Forum and Search --> : Replacing links with the full url


druss
12-15-2003, 02:46 AM
i am trying to replace links with the full url of the links.

i used the code below to get all links in the browser and what i want to do now is to replace those links with the url base also.

ive tried to use a simple search and replace method but it just keeps looping and if i was to have a link such as # in there then it will replace all my #000000 colors with the new link.

here is the code
[CODE]
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;

$ua = LWP::UserAgent->new;

# Set up a callback that collect image links
my @links = ();
sub callback2 {
my($tag, %attr) = @_;
push(@links, values %attr);
@test = @_;
}

# Make the parser. Unfortunately, we don't know the base yet
# (it might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback2);
# Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),
sub {$p->parse($_[0])});

@original_links = @links;
# Expand all image URLs to absolute ones
my $base = $res->base;
@links = map { $_ = url($_, $base)->abs; } @links;

@based_links = @links;

for($i = 0; $i <= $#original_links; $i++) {
s/$original_links[$i]/$based_links[$i]/g for @data;
}
[CODE]

is there an easier way of replacing the links like a perl module or something or am i over seeing a simple problem here??


Thanks
Goran

Phil Karras
12-26-2003, 03:22 PM
Using something like:

for $i ($#original_links) {
}

should limit it to only once through.