-1
您好,我在過濾文本文件中的某些行時遇到了問題。當我運行一個程序時,會生成一個純文本文件,列出與驗證文件相關的錯誤消息。不過,我需要忽略一些錯誤的產生,主要是那些進口或在搜索路徑中像下面的那些錯誤說明未發現:使用PHP過濾來自純文本文件的特定行
static-state.y:8: error: module "inet-types" not found in search path
mpls-static-state.y:11: error: module "y-types" not found in search path
mpls-static-state.y:11: warning: imported module y-types not used
mpls-static-state.y:15: error: module "context-state" not found in search path
mpls-static-state.y:15: warning: imported module context-state not used
mpls-static-state.y:19: error: module "contexts" not found in search path
會有,我需要讓我的其他錯誤信息不要完全清空文件,我也正在閱讀文本文件並在網頁上顯示這些錯誤。我從這樣的文件中讀取:
if(file_exists("stderr.txt")){
$fh = fopen("stderr.txt", 'r');
$errorOutput = fread($fh, 25000);
}
然後,我使用$ errorOutput來存儲文本文件中的內容。過濾我不想出來的錯誤行的最佳方法是什麼?試圖創建一個正則表達式,但沒有任何運氣得到它的工作有人可以幫助嗎?
命令行是或否?平臺?選擇標準?在Linux上,我只會使用_grep_。 – syck
嗨,這是在Windows上運行,文本文件是從運行bash腳本生成的。 PHP用於從它生成的位置讀取文本文件。我需要使用PHP從文本文件中刪除我不需要的錯誤消息,同時保留我需要的錯誤消息。 – olliejjc16
您可以使用錨點('^ $')和'm'修飾符,並且每行都會中斷搜索。然後,只需使用'preg_replace'和正則表達式匹配你不想要的行。''grep''v'標誌忽略找到的匹配。 – chris85