How to replace glossary terms in HTML text with links?
<p>I would like to run a <code>str_replace</code> or <code>preg_replace</code> which looks for certain words (found in <code>$glossary_terms</code>) in my <code>$content</code> and replaces them with links (like <code>&lt;a href="/glossary/initial/term"&gt;term&lt;/a&gt;</code>).</p>

<p>However, the <code>$content</code> is full HTML and my links/images are being affected too, which isn't what I'm after.</p>

<p>An example of <code>$content</code> is:</p>

<pre><code>&lt;div id="attachment_542" class="wp-caption alignleft" style="width: 135px"&gt;&lt;a href="http://www.seriouslyfish.com/dev/wp-content/uploads/2011/12/Amazonas-English-1.jpg"&gt;&lt;img class="size-thumbnail wp-image-542" title="Amazonas English" src="http://www.seriouslyfish.com/dev/wp-content/uploads/2011/12/Amazonas-English-1-288x381.jpg" alt="Amazonas English" width="125" height="165" /&gt;&lt;/a&gt;&lt;p class="wp-caption-text"&gt;Amazonas Magazine - now in English!&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Edited by Hans-Georg Evers, the magazine &amp;#8216;Amazonas&amp;#8217; has been widely-regarded as among the finest regular publications in the hobby since its launch in 2005, an impressive achievment considering it&amp;#8217;s only been published in German to date. The long-awaited English version is just about to launch, and we think a subscription should be top of any serious fishkeeper&amp;#8217;s Xmas list&amp;#8230;&lt;/p&gt;
&lt;p&gt;The magazine is published in a bi-monthly basis and the English version launches with the January/February 2012 issue with distributors already organised in the United States, Canada, the United Kingdom, South Africa, Australia, and New Zealand. There are also mobile apps availablen which allow digital subscribers to read on portable devices.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s fair to say that there currently exists no better publication for dedicated hobbyists with each issue featuring cutting-edge articles on fishes, invertebrates, aquatic plants, field trips to tropical destinations plus the latest in husbandry and breeding breakthroughs by expert aquarists, all accompanied by excellent photography throughout.&lt;/p&gt;
&lt;p&gt;U.S. residents can subscribe to the printed edition for just $29 USD per year, which also includes a free digital subscription, with the same offer available to Canadian readers for $41 USD or overseas subscribers for $49 USD. Please see the &lt;a href="http://www.amazonasmagazine.com/"&gt;Amazonas website&lt;/a&gt; for further information and a sample digital issue!&lt;/p&gt;
&lt;p&gt;Alternatively, subscribe directly to the print version &lt;a href="https://www.amazonascustomerservice.com/subscribe/index2.php"&gt;here&lt;/a&gt; or digital version &lt;a href="https://www.amazonascustomerservice.com/subscribe/digital.php"&gt;here&lt;/a&gt;. Just gonna add this to the end of the post so I can do some testing.&lt;/p&gt;
</code></pre>

<p>I came across <a href="http://stackoverflow.com/questions/3172493/str-replace-within-certain-html-tags-only">this link</a>, but I wasn't sure if such a method would work with nested HTML. </p>

<p>Is there any way I can <code>str_replace</code> or <code>preg_replace</code> content within <code>&lt;p&gt;</code> tags only; excluding any nested <code>&lt;a&gt;</code>, <code>&lt;img&gt;</code> or <code>&lt;h1/2/3/4/5&gt;</code> tags?</p>

<p>Thanks in advance,</p>
