2016-02-17 29 views
0

我有一個字符串,我會越來越這麼多的HTML標記,我想用空間來取代他們,我們。如何能做到這一點請建議我。這是我的字符串:正則表達式替換所有的HTML標籤,除了BR和p標籤perl的

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

我已經試過,但這個是沒有相應的工作:

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

強制性鏈接,所有的問題,要求正則表達式解析XML:http://stackoverflow.com/a/1732454/18157 –

+0

@JimGarrison什麼我在我的情況下做的,請幫助在 –

+0

爲什麼不[用strip_tags ()](http://php.net/strip_tags) –

回答

-1

在你的正則表達式,你沒有提到替代字符或分隔符。在你的情況下,你應該用空間替代。 正則表達式是:

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

由於這個答案現在站立,這是非常* *低質量(以及它的錯誤) –

1

您需要處理結束標記:

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; 

您還可以使用包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);