I would like to be able to switch this…
My sample [a id='keyword' href='someURLkeyword'] test keyword test[/a] link this keyword here.
To…
My sample [a id='keyword' href='someURLkeyword'] test keyword test[/a] link this [a href='url']keyword[/a] here.
I can’t simply replace all instances of ‘keyword’ because some are used in or within an existing anchor tag.
Note: Using PHP5 preg_replace on Linux.
Using regular expressions may not be the best way to solve this problem, but here is a quick solution:
The general idea is to split the string into links and everything else. Then split everything outside of a link tag into tags and plain text and insert links into the plain text. That will prevent [p class=’keyword’] from being expanded to [p class='[a href=’url’]keyword[/a]’].
Again, I would try to find a simpler solution that does not involve regular expressions.