search

Home  >  Q&A  >  body text

How to delete a line from a file with php?

<p>I have a file called <code>$dir</code> and a string called <code>$line</code> which I know is the complete line of the file , but I don't know its line number and I want to delete it from the file, what should I do? </p> <p>Can I use awk? </p>
P粉489081732P粉489081732444 days ago519

reply all(2)I'll reply

  • P粉952365143

    P粉9523651432023-08-28 16:35:58

    Read lines one by one and write all but the matching lines to another file. Then replace the original file.

    reply
    0
  • P粉107772015

    P粉1077720152023-08-28 10:25:18

    $contents = file_get_contents($dir);
    $contents = str_replace($line, '', $contents);
    file_put_contents($dir, $contents);

    reply
    0
  • Cancelreply