Replace link urls with # serverside
<p>I wish to replace all the link urls in some HTML with a number sign (#).</p>

<p>The following basically works, however, it to my dismay inserts a <code>&lt;!DOCTYPE&gt;</code>, <code>&lt;html&gt;</code>, and <code>&lt;body&gt;</code> tag around the modified HTML.  Is it possible to keep these tags from being inserted?  Is there a better way to do this?</p>

<p>Thank you</p>

<pre><code>$html_with_urls = '&lt;p&gt;hello.  Here is a &lt;a href="http://somesite.com"&gt;link&lt;/a&gt;.  Goodby&lt;/p&gt;';
libxml_use_internal_errors(true); //Temorarily disable errors resulting from improperly formed HTML
$doc = new DOMDocument();
$doc-&gt;loadHTML($html_with_urls);
$a = $doc-&gt;getElementsByTagName('a');
foreach ($a as $link) {
    if ($link-&gt;hasAttribute('href')) {
        $link-&gt;setAttribute('href', '#');
    }
}
$html_without_urls = $doc-&gt;saveHTML();
libxml_use_internal_errors(false);
echo($html_with_urls . '&lt;br /&gt;' . $html_without_urls); 
</code></pre>
