use HTML::Parser; my (%tags_1) = map { (lc($_), 1) } qw ( A B BR P ); sub HTMLSafeContent { my ($content, $tags) = @_; my ($safe_content); my ($Tag) = sub { my ($tagname, $attr, $offset, $length) = @_; if ($tags->{$tagname}) { $safe_content .= substr($content,$offset,$length); } }; my ($Text) = sub { my ($text) = @_; $safe_content .= $text; # Or, perhaps: # $safe_content .= URLIfy($text); }; my ($hp) = new HTML::Parser ( api_version => 3, start_h => [ $Tag, 'tagname,attr,offset,length' ], end_h => [ $Tag, 'tagname,attr,offset,length' ], text_h => [ $Text, 'dtext' ], ); $hp->parse($content); return $safe_content; } # Using it: # Load content into $some_content print HTMLSafeContent ($some_content, \%tags_1);