use HTML::TreeBuilder;
sub strip_a_elements
{
my( $html, $preserve_formatting ) = @_;
my $t = HTML::TreeBuilder->new;
if ( $preserve_formatting )
{
$t->no_space_compacting(1);
$t->ignore_ignorable_whitespace(0);
$t->store_comments(1);
$t->store_declarations(1);
$t->store_pis(1);
}
$t->parse( $html )->eof;
# we've parsed; now do the desired transformation:
$_->replace_with_content for $t->find_by_tag_name('a');
# and return the resulting hunk of html:
$t->as_HTML
}