Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: split html via HTML::Parser

by saskaqueer (Friar)
on Feb 28, 2005 at 12:11 UTC ( [id://435032]=note: print w/replies, xml ) Need Help??


in reply to split html via HTML::Parser

#!/usr/bin/perl -w use strict; use HTML::Parser; my $parser = HTML::Parser->new( start_h => [ \&_starttag, 'self, tagname, attr' ], end_h => [ \&_endtag, 'self, tagname' ], text_h => [ \&_text, 'self, dtext' ] ); my @chunks; $parser->parse_file(\*DATA); print "----------\n$_\n----------\n\n" for @chunks; sub _starttag { my ($self, $tag, $attr) = @_; $self->{'_pre'} = 1 if ($tag eq 'pre'); } sub _endtag { my ($self, $tag) = @_; $self->{'_pre'} = undef if ($tag eq 'pre'); } sub _text { my ($self, $dtext) = @_; $dtext =~ s/\A\s+//; $dtext =~ s/\s+\z//; return() unless ( length($dtext) > 0 and $dtext =~ /[^\s]/ ); if ( defined($self->{'_pre'}) ) { push(@chunks, "PRE: $dtext"); } else { push(@chunks, "TEXT: $dtext"); } } __END__ <p>This is a bad try to display text then code <pre>#! usr/bin/perl use strict; use warnings; print "Hello World!";</pre> and then plain text again</p>

Replies are listed 'Best First'.
Re^2: split html via HTML::Parser
by reneeb (Chaplain) on Feb 28, 2005 at 12:22 UTC
    This works fine! Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-03-28 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found