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]| )*<\s*/\s*b\s*>##ig;
print $text;
__DATA__
<p>test
<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 ></B>
<p>test<B
> </B >
<p>test<B> </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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|