Edit file line by line
<p>I'm making a program that reads a file line by line and deletes the lines that say <code>Hand #[any numbers]</code></p>
<pre><code>$file_name = 'the_file.txt';
$file_handle = fopen($file_name,&quot;r+&quot;);

while(! feof($file_handle)) {
    $file = fgets($file_handle);

    if (preg_match(&quot;/^(Hand\s#\d+)/m&quot;, $file)) {
        fwrite($file_handle, preg_replace(&quot;/^(Hand\s#\d+)/m&quot;, &quot;&quot;, $file));
    }
    
  }

fclose($file_handle);
</code></pre>
<p>This code does nothing however if put <code>echo $file . '&lt;br /&gt;';</code> It prints every line with <code>Hand #[any numbers]</code> So think its a problem with <code>fwrite($file_handle, preg_replace(&quot;/^(Hand\s#\d+)/m&quot;, &quot;&quot;, $file));</code> Which work fine if the file is not read line by line.</p>