Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to replace string in a HTML file ??

Reply
Thread Tools

How to replace string in a HTML file ??

 
 
jussi
Guest
Posts: n/a
 
      10-10-2005
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:

javascriptopUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\110-JORDAN-AID.mpg@$$@110-JORDAN-AID',1)
javascriptopUpPlayFile('./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:

javascriptopUpPlayFile('./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

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      10-10-2005

"jussi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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:
>
> javascriptopUpPlayFile('./PlayFile.asp?E:\\Reuters\\video\\110-JORDAN-AID.mpg@$$@110-JORDAN-AID',1)
> javascriptopUpPlayFile('./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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
How find and replace a string in the HTML file jussi Perl Misc 5 10-07-2005 10:23 AM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments