I have html content stored in a database table. in that html content I want to replace “SOME WORDS” with a link tag. But if “SOME WORDS” is already inside a link tag i should omit them..
e.g.
The content
<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>
The output should be
<p>Lorem ipsum dolor <a href="http://someurl">SOME WORDS</a>, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>
as you can see, it should exclude existing link texts when replacing.
Some guidance to get in to the right track is very much appreciated.
This is how you could solve it using
DOMDocumentinstead of regular expressions: