Is this an attempt at HTML to text conversion? If so, this is what I was working on yesterday. Here's my code for solving that:

use HTML::TreeBuilder; use HTML::FormatText; use Encode; sub HTML_to_text { my $content = shift; my $html = HTML::TreeBuilder->new; $content = '<body>' . $content . '</body>' unless $content =~ m/<body[^>]*>/i; $html->parse( decode("utf8", $content) ); # this is necessary othe +rwise UTF8 chars get hamburgered, my $formatter = HTML::FormatText->new; my $out = _trim($formatter->format($html)); # trim is a selective + trimmer that preserves some kinds of whitespace, delete this if you +don't need it. return $out; }
(The ridiculous number of lines and named variables are due to a step-by-step debugging inspection where I was trying to solve the utf8 problem -- essentially, make sure you have the latest HTML::Parser installed if you anticipate utf8 characters, and you may need to have them "marked" as such by going through the decode() function.)

If you only need to remove *some* tags, try HTML::TagFilter.


In reply to Re: Using HTML::Parser for simple tag removal by rlucas
in thread Using HTML::Parser for simple tag removal by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.