in reply to HTML::Element and "<br>" tags
I found some code that does just that in a script I use. It uses the XML::Twig interface, for I prefer that to HTML::Tree. Here's the subroutine, plus some code to show how it should be invoked. Of course, you may need to also teach this about some elements other than br.
use warnings; use strict; use 5.010; use XML::Twig; sub html_text { my($el) = @_; my $r = ""; for my $n ($el->descendants_or_self) { if ($n->is_text) { $r .= $n->trimmed_text; } elsif ("br" eq $n->gi) { $r .= "\n"; } } $r; } my $tw = XML::Twig->new; $tw->parse_html(q(<p>The Cow Jumped<br>Over the Moon</p>)); say html_text($tw->root);
|
|---|