如何在 PHP 中显示文本文档的所有行,但特定行除外


How can i show all lines of a text document except a specific line in PHP?

$file=fopen("question.txt","r");
while(!feof($file))
{    
  echo "<h3>". fgets($file)."</h3>"."<br />";
  for($i=0;$i<=3;$i++)
  {
    echo fgets($file)."<br />";
  }
}

$lines = file ("filename.txt");

然后$lines[4]会返回第五行。

查找并替换该行。看看str_replace函数 http://php.net/manual/en/function.str-replace.php :)

在这种情况下

也许是更好的选择...使用 file($path) 函数将行放入数组中,然后遍历它。

$lines = file($path, FILE_IGNORE_NEW_LINES);
$remove = "balblalbllablab";
foreach($lines as $key => $line)
  if(stristr($line, $remove)) unset($lines[$key]);
$data = implode(''n', array_values($lines));
$file = fopen($path);
fwrite($file, $data);
fclose($file);