Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Answer: how-to strip empty HTML tags like b /b

( #110464=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > how-to strip empty HTML tags like <b> </b> contributed by tachyon

The normal answer would be use HTML::TokeParser if you want a reliable solution to parse HTML. For this *particular* task a well constructed regex should suffice. This will strip all the <b> </b> tags that are empty. I believe it covers all bases.

$text = join '', <DATA>; print $text; $text =~ s#<\s*b\s*>(?:[\s\n]|&nbsp;)*<\s*/\s*b\s*>##ig; print $text; __DATA__ <p>test <p>test<b></b> <p>test<b >&nbsp; </b> <p>test<b> </b> <p>test<b ></b > <p>test<B></B> <p>test<B> </B> <p>test<B ></B> <p>test<B > </B > <p>test<B> &nbsp; &nbsp; </B> <p><b>I am not empty!</b>

Comment on Answer: how-to strip empty HTML tags like b /b
Download Code
Replies are listed 'Best First'.
Re: Answer: how-to strip empty HTML tags like b /b
by Hofmator (Curate) on Sep 06, 2001 at 16:33 UTC

    I'd not replace the non-breaking spaces, maybe you could remove the bold tags, but removing the nbsp can mess up the layout of a table.

    Just my euro 0.02

    -- Hofmator

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2016-05-26 07:13 GMT
Find Nodes?
    Voting Booth?