<p>What's the best way to take some plain text (not PHP code) which contains PHP-style variables, and then substitute in the value of the variable. This is kinda hard to describe, so here's an example.</p>

<pre><code>// -- myFile.txt --
Mary had a little $pet.

// -- parser.php --
$pet = "lamb";
// open myFile.txt and transform it such that...
$newContents = "Mary had a little lamb.";
</code></pre>

<p>I've been considering using a regex or perhaps <code>eval()</code>, though I'm not sure which would be easiest. This script is only going to be running locally, so any worries regarding security issues and <code>eval()</code> do not apply <em>(i think?)</em>.</p>

<p>I'll also just add that I can get all the necessary variables into an array by using <code>get_defined_vars()</code>:</p>

<pre><code>$allVars = get_defined_vars();
echo $pet;             // "lamb"
echo $allVars['pet'];  // "lamb"
</code></pre>

Here's what I've just come up with, but I'd still be interested to know if there's a better way. Cheers.
```
```
$allVars = get_defined_vars();
$file = file_get_contents('myFile.txt');

foreach ($allVars as $var => $val) {
    $file = preg_replace("@\\$" . $var . "([^a-zA-Z_0-9\x7f-\xff]|$)@", $val . "\\1", $file);
}
```
```