0

I have a string in that i will be getting so many html tag i want to replace them with space .How can we do this please suggest me .This is my string :

Wrong html <a> </I> <p>My paragraph</p> <i>Italics</i> <p class="blue">second</p> and the string is <br> after that test.

I have tried this but this is not working accordingly:

$string =~ s/(<((?!br|p)[^>]+)>)//ig;
Pooja Dubey
  • 683
  • 2
  • 14
  • 34

2 Answers2

1

You need to deal with closing tags:

use Modern::Perl;

my $str = 'Wrong html <a> </I> <p>My paragraph</p> <i>Italics</i> <p class="blue">second</p> and the string is <br> after that test.';

$str =~ s~<(?!/?\s*br|/?\s*p)[^>]+>~~ig;
say $str;

You could also use package HTML::StripTags:

use HTML::StripTags qw(strip_tags);

my $str = 'Wrong html <a> </I> <p>My paragraph</p> <i>Italics</i> <p class="blue">second</p> and the string is <br> after that test.';
my $allowed_tags = '<p><br>';

say strip_tags( $str, $allowed_tags );
Toto
  • 89,455
  • 62
  • 89
  • 125
-1

In your regular expression, you did not mention substitution character or delimiter. In your case, you should substitute with space. Regular expression is:

$msg =~s/(<((?!br|p)[^>]+)>)/ /ig;
Sandeep
  • 51
  • 3