Trying to escape $, not working as I expected?
<p>I'm trying to escape the character <code>$</code> so that it becomes a literal within a string I'm compiling. I thought that this would do the trick, but apparently not:</p>

<pre><code>$html = $_POST['html'];
$sanitize = htmlspecialchars($html);
$sanitize = str_replace("$", "\$", $sanitize); // Addition.
</code></pre>

<p>Here's my base code posted as <code>html</code> (it was originally a sanitizer for html, the last part being an addition).</p>

<pre><code>$rp = realpath($_SERVER['DOCUMENT_ROOT']);
include($rp. "_static/inc/db_conn.php");

$conn = mysql_connect($db_host, $db_user, $db_pass); mysql_select_db($db_name);
</code></pre>

<p>It produces:</p>

<pre><code>$rp = realpath($_SERVER[\'DOCUMENT_ROOT\']);
include($rp. \"_static/inc/db_conn.php\");

$conn = mysql_connect($db_host, $db_user, $db_pass); mysql_select_db($db_name);
</code></pre>

<p>It appears thus, that <code>htmlspecialchars()</code> is working as I'd expect it to, but not <code>str_replace()</code>.</p>

<p>Any help/answers would be appreciated (heads up, I've never used <code>str_replace()</code> before, so I just went as per the PHP doc).</p>
