How to replace string in a HTML file ??

J

jussi

Hi gurus


I asked this before but becose I made my self unclear I asking again. I
hope I'm able to present this time my problem.


I have an .ASP html file in a server. I need to read the file and then
replace some strings inside it. Those strins are founded from scalar
and I know the name to match in the orginal HTML file.

Here is the script so far:

------------- START ----------------

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use HTML::TreeBuilder;

open(REUTERS,"> /var/www/html/FileSearch.asp");

#Get the orginal file
my $html = get("http://10.10.10.158/FileSearch.asp");
die "Couldn't get it!" unless defined $html;


#Use HTML::TreeBuilder to find the right string
my $tree = HTML::TreeBuilder->new();
$tree->parse($html);

# find elements containing: src="images/play_ico.gif"
foreach my $img ( $tree->look_down('src',
'images/play_ico.gif') ) {
next unless $img->tag eq 'img';
next unless $img->parent->tag eq 'a';
my $href = $img->parent->attr('href');
print "$href\n";
}

------------- END ---------------------

Ok. When I run this, its prints out:

javascript:popUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\110-JORDAN-AID.mpg@$$@110-JORDAN-AID',1)
javascript:popUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\109-INDIA-KASHMIRRELIEFCAMP.mpg@$$@109-INDIA-KASHMIR%CH20RELIEF%CH20CAMP',1)

These are string I want to change for playing directly from network
drive:

"file://videoserver/mpg1/"110-JORDAN-AID.mpg"
"file://videoserver/mpg1/109-INDIA-KASHMIRRELIEFCAMP.mpg"

So the BIG question is, how do I search a string and replace it ??

It shoul do like:

-------------- START -------------

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use HTML::TreeBuilder;

open(REUTERS,"> /var/www/html/FileSearch.asp");

#Get the orginal file
my $html = get("http://10.10.10.158/FileSearch.asp");
die "Couldn't get it!" unless defined $html;

my $videoname = "110-JORDAN-AID.mpg";

## So we need to find string:

javascript:popUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\$videoname@$$@110-JORDAN-AID',1)

## And replace with this one:

file://videoserver/mpg1/"110-JORDAN-AID.mpg

------------ END ---------------------


Any hints would be very nice.
-Jussi
 
M

Matt Garrish

jussi said:
I have an .ASP html file in a server. I need to read the file and then
replace some strings inside it. Those strins are founded from scalar
and I know the name to match in the orginal HTML file.

Here is the script so far:

------------- START ----------------

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use HTML::TreeBuilder;

open(REUTERS,"> /var/www/html/FileSearch.asp");

#Get the orginal file
my $html = get("http://10.10.10.158/FileSearch.asp");
die "Couldn't get it!" unless defined $html;

You do understand that you are getting an *html* page, right? Asp coding is
invisible to the end user as all that is returned is html, so why are you
saving the page you retrieve with an asp extension?
#Use HTML::TreeBuilder to find the right string
my $tree = HTML::TreeBuilder->new();
$tree->parse($html);

# find elements containing: src="images/play_ico.gif"
foreach my $img ( $tree->look_down('src',
'images/play_ico.gif') ) {
next unless $img->tag eq 'img';
next unless $img->parent->tag eq 'a';
my $href = $img->parent->attr('href');
print "$href\n";
}

------------- END ---------------------

Ok. When I run this, its prints out:

javascript:popUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\110-JORDAN-AID.mpg@$$@110-JORDAN-AID',1)
javascript:popUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\109-INDIA-KASHMIRRELIEFCAMP.mpg@$$@109-INDIA-KASHMIR%CH20RELIEF%CH20CAMP',1)

These are string I want to change for playing directly from network
drive:

"file://videoserver/mpg1/"110-JORDAN-AID.mpg"
"file://videoserver/mpg1/109-INDIA-KASHMIRRELIEFCAMP.mpg"

So the BIG question is, how do I search a string and replace it ??

Why is that a big question? You've obviously found the attributes you want
to replace, so just run a regular expression on them. I get the impression
that this is an attempt to slowly get other people to write a script for
you.

If you don't know regular expression syntax, take some time to read perlre,
perlretut, and perlrequick. You may get help with your regular expressions
here, but you'll not likely find anyone who's going to write them for you.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top