I am using preg_replace to generate contextual links in text blocks using the following code:
$contextualLinkStr = 'mytext';
$content = 'My text string which includes MYTEXT in various cases such as Mytext and mytext. However it also includes image tags such as <img src="http://www.myurl.com/mytext-1.jpg">';
$content = preg_replace('/' . $contextualLinkStr . '/i ', '<a href="foo.html">\\0</a>', $content);
I have a trailing space in my preg_replace in my search pattern. This is because I want the function to ignore any instances of mytext that appear in image links. This works but causes a problem with the output. The output also carries the trailing space as follows:
<a href="foo.html">mytext </a>
So my question is - How do I remove the trailing space from within the link on the output and then add a trailing space after the link to account for the already removed space. I need the output to be as follows:
<a href="foo.html">mytext</a>_
Where "_" is the trailing space. Can anybody please help?
Thanks,
Jason.