PHP - Find/Replace Text in RTF/txt files <p>I'm running into an issue with finding specific text and replacing it with alternative text. I'm testing my code below with <code>.rtf</code> and <code>.txt</code> files only. I'm also ensuring the files are writable, from within my server. </p> <p>It's a hit and miss situation, and I'm curious if my code is wrong, or if this is just weirdness of opening and manipulating files.</p> <pre><code>&lt;?php $filelocation = '/tmp/demo.txt'; $firstname = 'John'; $lastname = 'Smith'; $output = file_get_contents($filelocation); $output = str_replace('[[FIRSTNAME]]', $firstname, $output); $output = str_replace('[[LASTNAME]]', $lastname, $output); $output = str_replace('[[TODAY]]', date('F j, Y'), $output); // rewrite file file_put_contents($filelocation, $output); ?&gt; </code></pre> <p>So, inside the <code>demo.txt</code> file I have about a full page of text with [[FIRSTNAME]], [[LASTNAME]], and [[TODAY]] scattered around.</p> <p>It's hit and miss with the find/replace. So far, [[TODAY]] is always replaced correctly, while the names aren't.</p> <p>Has anyone had this same issue? </p> <p>(on a side note, I've checked error logs and so far no PHP warning/error is returned from opening the file, nor writing it)</p>