Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Replacing an HTML element with multiple elements using HTML::TreeBuilder

by tangent (Vicar)
on Jul 03, 2019 at 16:08 UTC ( #11102364=note: print w/replies, xml ) Need Help??


in reply to Replacing an HTML element with multiple elements using HTML::TreeBuilder

Not sure if I understand your requirements fully, but this works for me:
for my $p ($xhtml->findnodes('//blockquote/p')) { my $text = $p->as_text(); $text =~ s/^\s+//; $text =~ s/\s+$//; if ( $text =~/\n\s*\n\s*/ ) { my @paragraphs = split(/\s*\n\s*/, $text); my @new_elems; for my $para (@paragraphs) { my $new = HTML::Element->new('p'); $new->push_content($para); push(@new_elems, $new); } $p->replace_with(@new_elems); } }
  • Comment on Re: Replacing an HTML element with multiple elements using HTML::TreeBuilder
  • Download Code

Replies are listed 'Best First'.
Re^2: Replacing an HTML element with multiple elements using HTML::TreeBuilder
by mldvx4 (Monk) on Jul 03, 2019 at 17:52 UTC

    Thanks. I had tried several approaches using replace_with, but each met with various kinds of failure. Your example works and gives me a bit more of an idea how to use the methods.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102364]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-01-21 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?