How to replace string in a HTML file ??

Discussion in 'Perl Misc' started by jussi, Oct 10, 2005.

  1. jussi

    jussi Guest

    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
     
    jussi, Oct 10, 2005
    #1
    1. Advertising

  2. jussi

    Matt Garrish Guest

    "jussi" <> wrote in message
    news:...
    >
    > 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
     
    Matt Garrish, Oct 10, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Umesh
    Replies:
    17
    Views:
    894
  2. Alun
    Replies:
    3
    Views:
    4,649
    Masudur
    Feb 18, 2008
  3. Prasad S
    Replies:
    2
    Views:
    257
    Dr John Stockton
    Aug 27, 2004
  4. jussi
    Replies:
    5
    Views:
    576
    Gunnar Hjalmarsson
    Oct 7, 2005
  5. V S Rawat
    Replies:
    5
    Views:
    345
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page