22 Apr 2016 11:21:38 UTC
- Distribution: HTML-Strip
- Module version: 2.10
- Source (raw)
- Browse (raw)
- How to Contribute
- Issues (4)
- Testers (5716 / 15 / 3)
- KwaliteeBus factor: 2
- 94.81% Coverage
- License: perl_5
- Perl: v5.8.0
- Activity24 month
- Download (14.97KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version
HTML::Strip - Perl extension for stripping HTML markup from text.
use HTML::Strip; my $hs = HTML::Strip->new(); my $clean_text = $hs->parse( $raw_html ); $hs->eof;
This module simply strips HTML-like markup from text rapidly and brutally. It could easily be used to strip XML or SGML markup instead; but as removing HTML is a much more common problem, this module lives in the HTML:: namespace.
It is written in XS, and thus about five times quicker than using regular expressions for 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 caveat that a
>character may appear in either of the following without ending the tag:
Quotes are considered to start with either a
"character, and end with a matching character not preceded by an even number or escaping slashes (i.e.
\"does not end the quote but
If the tag starts with an exclamation mark, it is assumed to be a declaration or a comment. Within such tags,
>characters do not end the tag if they appear within pairs of double dashes (e.g.
<!-- <a href="old.htm">old page</a> -->would be stripped completely). No parsing for quotes is performed within comments, so for instance
<!-- comment with both ' quote types " -->would be entirely stripped.
Anything the appears within what we term strip tags is stripped as well. By default, these tags are
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, quote, comment, or whatever; 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(). Alternatively, you may set
auto_resetto true on the constructor or any time after with
set_auto_reset, so that the parser will always operate in one-shot basis (resetting after each parsed chunk).
Constructor. Can optionally take a hash of settings (with keys corresponsing to the
For example, the following is a valid constructor:
my $hs = HTML::Strip->new( striptags => [ 'script', 'iframe' ], emit_spaces => 0 );
Takes a string as an argument, returns it stripped of HTML.
Resets the current state information, ready to parse a new block of HTML.
Clears the current set of strip tags.
Adds the string passed as an argument to the current set of strip tags.
Takes a reference to an array of strings, which replace the current set of strip tags.
Takes a boolean value. If set to false, HTML::Strip will not attempt any conversion of tags into spaces. Set to true by default.
Takes a boolean value. If set to false, HTML::Strip will decode HTML entities. Set to true by default.
If HTML::Entities is available, this method behaves just like invoking HTML::Entities::decode_entities, except that it respects the current setting of 'decode_entities'.
Sets a filter to be applied after tags were stripped. It may accept the name of a method (like 'filter_entities') or a code ref. By default, its value is 'filter_entities' if HTML::Entities is available or
Takes a boolean value. If set to true,
parseresets after each call (equivalent to calling
eof). Otherwise, the parser remembers its state from one call to
parseto another, until you call
eofexplicitly. Set to false by default.
Outputs extensive debugging information on internal state during the parse. Not intended to be used by anyone except the module maintainer.
Readonly accessors for their respective settings.
Despite only outputting one space character per group of tags, and avoiding doing so when tags are bordered by spaces or the start or end of strings, 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 may want to post-filter the output of HTML::Strip to remove excess whitespace (for example, using
tr/ / /s;). (This has been improved since previous releases, but is still an issue)
- HTML Entities
HTML::Strip will only attempt decoding of HTML entities if HTML::Entities is installed.
None by default.
Alex Bowley <firstname.lastname@example.org>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.