in reply to Using Tokeparser to replace image paths on retrieved remote html

well, I got it working.. not sure how well, but it does work now..

I realised I already had a hash of the images from when I fetched them earlier in the script, so I used that to find those images in the html and sustitute them with the new local path and image name. and I put the regex in an if statement so that if the image tag was originally a relative link, it would match on just the image name and replace that with the new path/image name.. like I said, it works, but it seems overcomplicated for what it does.

Here is the new piece of code. (from the last file open onwards.)

Thanks for anyone that read this far.

regards

Franki

open FILE, "$file" or die "Can't open receipt page html file for display $!\n"; flock(FILE,2) or die "cannot lock file: $!"; # now make all image paths local and absolute. undef $/; # enable "slurp" mode my $line = <FILE>; # whole file now here close (FILE); $line =~ s/\n/ /g; #Strip out potentially nasty stuff. $line =~ s/<embed(.*)?<\/embed>/<!-- removed embed \/\/-->/sig +; $line =~ s/<applet(.*)?<\/applet>/<!-- removed applet \/\/-->/ +sig; $line =~ s/<object(.*)?<\/object>/<!-- removed object +\/\/-->/sig; my ($image_path, $image_name, @images_list); foreach my $key (keys %imagefiles){ $image_name = basename($key); $image_path = dirname($key); # do some regex juju to replace old path/image name, # with new path and the same image name. if ($line =~ /$key/ig) { $line =~ s/$key/$images_url\/$image_name/ig; } else { $line =~ s/$image_name/$images_url\/$image_name/ig; } }# end of foreach. print "Content-type: text/html\n\n"; print $line; # This empties the url file after the display is successful # Since the final read doesn't alter the actual file, its # important to clear out the file after use so it can't # be accessed directly, instead of only via the script. open (FILE, ">$file"); flock(FILE,2) or die "cannot lock file: $!"; close (FILE); exit(0);
  • Comment on Re: Using Tokeparser to replace image paths on retrieved remote html
  • Download Code