在文本文件中搜索单词并删除包含该单词的段

时间:2014-02-04 09:54:25

标签: search replace language-agnostic text-files

我想搜索某个词,例如例如在href-part中的“a-files”,如果成功,我想删除整个段(在本例中为> html-tag)。

Don't delete this sentence. <a href="/a-files/X"> Click here - 1 </a> This too..
Don't delete this sentence. <a href="/a-files/Y"> Click here - 2 </a> This too..
Don't delete this sentence. <a href="/b-files/Z"> Click here - 3 </a> This too..

在上面的示例中,以下部分:

<a href="/a-files/X"> Click here - 1 </a>
<a href="/a-files/Y"> Click here - 2 </a>

应该删除。

我该怎么做? (PHP,Bash-Shell,Whatever ..)

P.S。可能的方法是:

--> Find the word, 
--> go left, 
--> find "<a", 
--> begin select, 
--> go right, 
--> find "</a>", 
--> end select and 
--> delete this part. 

哪种编程语言可以轻松完成?

P.S。请考虑:文件并不总是一个html文件。我选择了一个html示例,更容易解释。

1 个答案:

答案 0 :(得分:0)

您可以使用phpQuery。 stackoverflow中的first question在某种程度上与您的问题有关。

$document  = phpQuery::newDocumentFileHTML('[file location]'); 
$text = pq('div')->find('a-files')->remove()->end();

希望它有所帮助!