0
我無法弄清楚如何讓grep返回文件中找到的所有行。它計算正確的數字,但只打印最後一場比賽。grep不返回所有匹配行
$found = array();
$term = escapeshellarg(self::$term);
foreach(self::$files as $file)
{
if($count = exec("grep -i -c ".$term." ".$file))
{
$lines = exec("grep -i -n ".$term." ".$file);
$found[] = array('count'=>$count,'lines'=>$lines,'file'=>str_replace(self::$dir, '', $file));
}
}
self::$files = $found;
比方說self::$term = 'console.log'
。它在一個特定文件中找到三個匹配,這是正確的(該術語出現在測試文件的第16,21和31行),但$lines
僅打印:31: console.log(response);
。
我錯過了什麼?
有沒有其他的選擇,因爲passthru()似乎迴應結果? –
沒關係。使用'ob_start()','$ lines = ob_get_content()'和'ob_end_clean()'是我需要做的。謝謝! –