Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

by tachyon (Chancellor)
on Sep 06, 2001 at 04:56 UTC ( #110464=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > how-to strip empty HTML tags like <b> </b> - Answer 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>

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 questions are moot...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-02-18 07:58 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (251 votes). Check out past polls.