I am trying to convert specific keywords in text, which are stored in array, to the links.
Example text:
$text='This text contains many keywords, but also formated <a href="#keywords" title="keywords">keywords</a>.'
So now I want to convert the word keywords to the <a href="#keywords" title="keywords">#keywords</a>.
I used the very simple preg_replace function
preg_replace('/keywords/i',' <a href="#keywords">keywords</a> ',$text);
but obviously it converts to link also the string already formatted as a link, so I get a messy html like:
$text='This text contains many <a href="#keywords" title="keywords">keywords</a>, but also formated <a href="#<a href="#keywords" title="keywords">keywords</a>" title="<a href="#keywords" title="keywords">keywords</a>"><a href="#keywords" title="keywords">keywords</a></a>.'
Expected result:
$text='This text contains many <a href="#keywords" title="keywords">keywords</a>, but also formated <a href="#keywords" title="keywords">keywords</a>.'
Any suggestions?
THX
EDIT
We are one step from the perfect function, but still not working well in this case:
$text='This text contains many keywords, but also formated
<a href="http://www.keywords.com/keywords" title="keywords">keywords</a>.'
In this case it replaces also the word keywords in the href, so we again get the messy code like
<a href="http://www.<a href="http://www.keywords.com/keywords" title="keywords">keywords</a>.com/<a href="http://www.keywords.com/keywords" title="keywords">keywords</a>" title="keywords">keywords</a>
I’m not great with regular expressions, but maybe this one will work:
What I think it will do is ignore any instances of
#keywords,>keywords, and"keywordsand find the rest.EDIT:
After testing it out, it looks like that replaces the space before the word as well, and doesn’t work if
keywordsis the beginning of the string. It also didn’t preserve original capitalization. I have tested this one, and it works perfectly for me:The first three are replaced, preserving the original capitalization, and the last one is left untouched. This one uses a negative lookbehind and backreferences.
EDIT 2:
OP edited question. With the new example provided, the following regex will work:
This will replace all instances of
keywordsthat are not preceded by#,>,",., or/.