2012-08-06 72 views
0

我一直在尋找幾個小時的解決方案,使用PHP腳本刪除文件中的文本塊。從文件中刪除多行使用PHP

似乎有很多選項可用於刪除文本行,但不包含文本塊。我試圖使用preg_replace ("/^$start.*?$end/s", $replace, $fdata)以下內容,但尚未找到可行的解決方案。

我相信有人已經這樣做了,所以任何幫助將不勝感激。

$start = "# Copyright 2000-"; 
$end = "Agreement."; 

# This software product may only be used strictly in accordance 
# with the applicable written License Agreement. 
+6

你在寫這個軟件嗎?或者你試圖竊取的軟件? – Matt 2012-08-06 15:43:44

+1

現在,馬特讓他給他帶來懷疑,他可以開源他自己的軟件,或更新他或他的公司編寫的軟件許可證。 – VoronoiPotato 2012-08-06 15:45:19

+0

@VoroniPotato永遠不會! – Matt 2012-08-06 15:47:03

回答

5

你需要multi-line mode/m),否則你的正則表達式不會跨越多行捕獲。此外,你應該用preg_quote()逃脫你的正則表達式的參數,否則你可能會得到意外的結果(例如,在$end,它有一個點,這是一個正則表達式元字符,當你想要它匹配單個週期)。

$regex = "/^" . preg_quote($start, '/') .".*?". preg_quote($end, '/') . "/sm"; 
preg_replace ($regex, $replace, $fdata);