Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: Re: Re: Re: Strip HTML tags again

by amphiplex (Monk)
on Jul 01, 2002 at 13:18 UTC ( #178539=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Strip HTML tags again
in thread Strip HTML tags again

Hi ! I think this does what you want:
use HTML::Tagset; my %tags = %HTML::Tagset::isKnown; my $tagpattern = "(".join('|',keys %tags).")"; print STDERR "$tagpattern\n"; while (<>) { print strip_html_tags($_); } sub strip_html_tags { my $line = shift; $line =~ s/<\s*$tagpattern(?:\s*>|\s+[^>]*>)([^<]*)<\s*\/\1[^>]*>/$2 +/ig; return $line; }
I first create the string $tagpattern by putting a "|" between all known HTML tags and surrounding the whole thing with parantheses. This will give something like "(a|p|code.....)" and is used later in the subroutine to check for valid HTML tags.

The regex looks a bit complicated and I am sure that it can be written much better, but I believe it is sufficient for your cause.

Note that this will only work for tags that are on one line and could get you into trouble if there are < or > signs inside a tag (Don't know if this is possible in HTML).

update:

It would propably be a lot wiser to use Ovid's code then my homegrown regex.

---- kurt

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Strip HTML tags again
by dda (Friar) on Jul 01, 2002 at 13:38 UTC
    I really love your idea! Thanks!

    --dda

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-10-04 03:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (15 votes). Check out past polls.

    Notices?