Reputation: 5203
I would like to test content that is submitted by users to see if contains HTML or not, and I'd prefer to do it without having to write my own regex. Does anyone know of a module that does this or a good way to do this in perl?
Upvotes: 2
Views: 777
Reputation: 63932
You can check the HTML::Restrict module what allows restrict the content only to allowed tags.
Example:
use 5.012;
use strict;
use warnings;
use HTML::Restrict;
use Data::Dumper;
my @texts = map { { "has_html", 0, "text", $_ } }
split(/==cut-here==/, do{ local $/; <DATA> });
my $res = HTML::Restrict->new();
foreach my $text (@texts) {
my $tmp = $text->{text};
my $plain = $res->process($tmp);
$plain =~ s/\s//gs;
$tmp =~ s/\s//gs;
$text->{has_html} = $tmp cmp $plain ? "YES" : "NO";
}
say Dumper(\@texts);
__DATA__
<img src="image.jpg" alt="tricky>text" />
text with html
==cut-here==
plain
text here
==cut-here==again <!-- a > b --> with html==cut-here==
plain
will check 4 chunks of text and detect for html. If you configure the HTML::Restrict
you can check with "allowed" and "not allowed" HTML tags too.
Upvotes: 2