Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by tangent (Parson)
on Jul 03, 2019 at 16:08 UTC ( [id://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 (Friar) 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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found