NAME
HTML::Strip - Perl extension for stripping HTML markup from text.
SYNOPSIS
use HTML::Strip;
my $hs = HTML::Strip->new();
$hs->parse( $html );
$hs->eof;
DESCRIPTION
This module simply strips HTML-like markup from text in a very quick and brutal manner. It's written in XS, so is vastly more efficient than using regexes to accomplish the same task.
It does not do any syntax checking (if you want that, use HTML::Parser), instead it merely applies the following rules:
Anything that looks like a tag, or group of tags will be replaced with a single space character. Tags are considered to be anything that starts with a '<' and ends with a '>', with the caveats that:
- a
-
A '>' character can appear within quotes within the tag without ending it. Quotes are considered to start with either a ' or a " character, and end with a matching character not preceeded by an even number or escaping slashes (i.e. '\"' does not end the quote but '\\\\"' does.
- b
-
If the tag starts with an exclamation mark, it is assumed to be a declaration or a comment. '>' characters do not end the tag if they appear within pairs of double dashes (e.g. '<!-- <a href="old.htm">old page</a> -->").
Anything the appears within so-called 'strip tags' is stripped as well. By default, these tags are 'title', 'script', 'style' and 'applet'.
HTML::Strip maintains state between calls, so you can parse a document in chunks should you wish. If one chunk ends half-way through a tag, it will remember this, and expect the next call to parse to start with the remains of said tag. If this is not going to be the case, be sure to call $hs->eof() between calls to $hs->parse.
METHODS
- new()
-
Constructor. Takes no arguments.
- parse()
-
Takes a string as an argument, returns it stripped of HTML.
- eof()
-
Resets the current state information, ready to parse a new block of HTML.
-
Takes a reference to an array of strings, which replace the current set of strip tags.
LIMITATIONS
- Whitespace
-
Despite only outputting one space character per group of tags, HTML::Strip can often output more than desired; such as with the following HTML:
<h1> HTML::Strip </h1> <p> <em> <strong> fast, and brutal </strong> </em> </p>
Which gives the following output: " HTML::Strip Fast and brutal "
Thus, you will probably want to post-filter the output of HTML::Strip to remove excess whitespace.
- HTML Entities
-
HTML::Strip attempt no decoding of HTML entities. Use the imaginatively-named HTML::Entities (specifically, the decode_entities() method) for this purpose.
EXPORT
None by default.
AUTHOR
Alex Bowley <kilinrax@cpan.org>
SEE ALSO
perl, HTML::Parser, HTML::Entities
3 POD Errors
The following errors were encountered while parsing the POD:
- Around line 127:
'=item' outside of any '=over'
- Around line 144:
You forgot a '=back' before '=head2'
- Around line 167:
You forgot a '=back' before '=head2'