2012-06-25 58 views
-5

我需要在html文件中保留幾個html標記,但是要擦除所有其他標記。如何用Perl修改HTML文件:保留列表並刪除所有其他標記

腳本的邏輯是:

- if there is <li> or <ul> on the line, do nothing (=write same line to output) 
- otherwise if there is html tag, remove it (=just write the content) 

可能有人請幫助我,這正好在我的非常有限的Perl技能。

+2

並不想成爲討厭...但你需要向我們展示的東西你即使它試圖超過了你的perl技能。 – PinkElephantsOnParade

回答

3

爲此,您可以用HTML::Restrict

#!/usr/bin/env perl 

use strict; 
use warnings; 

use HTML::Restrict; 

my $hr = HTML::Restrict->new(rules => { li => [], ul => [] }); 

my $html 
    = q[<body><b>hello</b> <img src="pic.jpg" alt="me" id="test" /><ul><li>one</li></ul></body>]; 
my $processed = $hr->process($html); 

print $processed; 

輸出的結果是:

hello <ul><li>one</li></ul> 
相關問題