Forum Moderators: coopster
Currently using the following regular expression to find the word "keyword" :-
[^>]\bkeyword\b[^</a>]
But it still matches the word "keyword" inside <a href="http://example.com">abc keyword xyz</a>.
Any suggestions to improve my RegExp pattern?
Milan
$noLinks = preg_replace('{<a.*?</a>}', '', $origText);
$noTags = preg_replace('{<.*?>}', '', $noLinks);
$noTags should have everything inside the original text minus hyperlinks and html tags. Hope this helps.
The word "keyword" should be replaced with hyperlink in the following string
"this is a keyword"
but not in any of the following
"this is a <a href="http://example.com">keyword</a> and another <a href="http://example.com">abc keyword def</a>"
This expression [^>]\bkeyword\b[^</a>] is still matching the "keyword" in second anchor text in the above example.
Milan
'One keyword, <em>second keyword</em>, <a href="example">third keyword</a>'
and change it into:
'One <a href="linktokeyword">keyword</a>, <em>second <a href="linktokeyword">keyword</a></em>, <a href="example">third keyword</a>'
Is that it? BTW, the regex part [^</a>] matches any single character that is not <, /, a, or >. Not any string that isn't </a>.
// Replace all keyword by hyperlink.
$step1 = preg_replace('{\bkeyword\b}', '<a href="link">keyword</a>', $origText);
// Remove inner hyperlinks created by step1 above.
$step2 = preg_replace('{(<a[^<]*)<a href="link">keyword</a>([^<]*</a>)}', '\1keyword\2', $step1);
This works as long as there are no other html tags inside the original hyperlinks.
[edited by: eelixduppy at 5:25 pm (utc) on Feb. 21, 2008]