in reply to Replacing text between two html comments
#! /usr/bin/perl use strict; use warnings; use HTML::TreeBuilder; my $new_div = HTML::Element->new_from_lol( [ q{div}, {id => q{memberships}}, [q{p}, q{new stuff}] ], ); my $p = HTML::TreeBuilder->new_from_file(*DATA) or die qq{H::TB new failed\n}; my $memberships_div = $p->look_down( _tag => q{div}, id => q{memberships}, ) or die qq{memberships div not found\n}; $memberships_div->replace_with($new_div); print $p->as_HTML(undef, q{ }, {}); __DATA__ <html><head><title>replace test</title></head> <body> <p>keep</p> <p>keep</p> <div id="memberships"> <p>stuff to replace</p> </div> <div id="banquet"> <p>all about the banquet</p> </div> <p>keep</p> <p>keep</p> </body> </html>
If you haven't come across it before there is a bit of a learning curve but if you need to work with HTML it is well worth it. I avoid using regexes on HTML at all costs. Here there be dragons. :-)<html> <head> <title>replace test</title> </head> <body> <p>keep</p> <p>keep</p> <div id="memberships"> <p>new stuff</p> </div> <div id="banquet"> <p>all about the banquet</p> </div> <p>keep</p> <p>keep</p> </body> </html>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replacing text between two html comments
by metaperl (Curate) on Jul 29, 2010 at 15:20 UTC |