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

how-to strip empty HTML tags like <b> </b>

( #110456=categorized question: print w/ replies, xml ) Need Help??
Contributed by russmann on Sep 06, 2001 at 04:03 UTC
Q&A  > regular expressions


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

A more complete option is not Perl. Use HTML Tidy which is by w3c's Dave Raggett. I just tested it and it will strip empty tags a well as a doing many other useful things. Get your free copy here http://www.w3.org/People/Raggett/tidy/ or get a Win32 GUI version here http://perso.wanadoo.fr/ablavier/TidyGUI/

Answer: 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>
Answer: how-to strip empty HTML tags like b /b
contributed by Hofmator

I'd also go with tachyon's suggestion of HTML Tidy, but if you are trying to do this quick and dirty somewhere in the middle of a script, I'd use this regex $text =~ s#<\s*([^>]*)\s*>[\s\n]*<\s*/\s*\1\s*>##ig;
It should remove any empty tags which don't contain any attributes (not just bold tags), so it works on

__DATA__ <i> </ I> < B ></b> < em> < / eM >

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (5)
    As of 2014-10-22 04:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (112 votes), past polls