title: Replace string in text file using PHP
body: <p>I need to open a text file and replace a string. I need this</p>
  
  <pre><code>Old String: &lt;span id="$msgid" style="display: block;"&gt;
  New String: &lt;span id="$msgid" style="display: none;"&gt;
  </code></pre>
  
  <p>This is what I have so far, but I don't see any changes in the text file besides extra white spaces.</p>
  
  <pre><code>$msgid = $_GET['msgid'];
  
  $oldMessage = "";
  $deletedFormat = "";
  
  // Read the entire string
  $str = implode("\n", file('msghistory.txt'));
  
  $fp = fopen('msghistory.txt', 'w');
  
  // Replace something in the file string - this is a VERY simple example
  $str = str_replace("$oldMessage", "$deletedFormat", $str);
  
  fwrite($fp, $str, strlen($str));
  fclose($fp);
  </code></pre>
  
  <p>How can I do it?</p>