Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Split tags and words nicely

by osunderdog (Deacon)
on Dec 28, 2006 at 13:43 UTC ( #592042=note: print w/replies, xml ) Need Help??


in reply to Split tags and words nicely

Here's an example using HTML::Parser

use strict; use HTML::Parser; use Data::Dumper; my $input = q{<tag ref=1>Start<tag ref=2>and more</tag>and end</tag>}; print "Input: [$input]\n"; my $p = HTML::Parser->new(api_version=>3, start_h=>[ \&startTokenHandler, "self,tokens" ], end_h=>[ \&endTokenHandler, "self,tokens" ], text_h =>[ \&textHandler, "self,dtext" ], ); $p->parse($input); sub startTokenHandler { my $self = shift; my $token = shift; printf("<%s %s=%d>\n", @$token); } sub endTokenHandler { my $self = shift; my $token = shift; printf("</%s>\n", $token->[0]); } sub textHandler { my $self = shift; my $text = shift; print "$text\n"; }

sample output:

$perl sample2.pl Input: [<tag ref=1>Start<tag ref=2>and more</tag>and end</tag>] <tag ref=1> Start <tag ref=2> and more </tag> and end </tag>

Hazah! I'm Employed!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2020-12-05 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (63 votes). Check out past polls.

    Notices?