0
我有一個文件,我用php
閱讀:搜索在PHP文件爲一個字符串,並返回下一行
$lines = file("input.txt");
我需要尋找$lines
的字符串--><--
然後返回第二天全線。做這個的最好方式是什麼?
謝謝。
我有一個文件,我用php
閱讀:搜索在PHP文件爲一個字符串,並返回下一行
$lines = file("input.txt");
我需要尋找$lines
的字符串--><--
然後返回第二天全線。做這個的最好方式是什麼?
謝謝。
您應該遍歷數組$lines
,然後將該鍵加1以獲得下一行。
$lines = file("input.txt");
$return = array();
foreach($lines as $key => $line) {
if(false !== strpos($line,'--><--')) {
// check if there is another line in the file
if(isset($lines[($key+1)])) {
// add the line to array
$return[] = $lines[($key+1)];
}
}
}
print_r($return);
我不知道,如果你匹配一個Windows或Unix文件(因此\r?
),但你可以使用正則表達式更乾淨做到這一點。你可以這樣捕獲它:
$lines = file("input.txt");
preg_match('!(--><--\r?\n)([^\r\n]+)\r?\n!s',$lines,$matches);
$myLine = $matches[2];
echo $myLine;
'grep -A 1「 - >< - 」input.txt' :) – Orangepill