I’ve been trying to do a regex to match and replace the occurrences of a keyword on a portion of HTML:
- i want to match
keywordand<strong>keyword</strong> - but
<a href="someurl.html" target="_blank">keyword</a>and<a href="someur2.html">already linked keyword </a>should NOT be matched
I’m only interested in matching (and replacing) the keyword on the 1st line.
The reason I want this is to replace keyword with <a href="dictionary.php?k=keyword">keyword</s>, but ONLY if keyword it’s not already inside an <a> tag.
Any help will be much appreciated!
I managed to do what I wanted (without using Regex) by:
<a>tags (copying them to a temporary array and keeping a placeholder on the string)str_replacethe new string in order to replace all the keywords<a>tagsHere’s the code I used, in case someone else needs it:
The output:
Links Array:
Phrase Holder:
(pre) Final Phrase (with my keyword replaced):
Final Phrase: