Forum Moderators: coopster
The regex I have does not fully cater for this. Could you please help me!
Regex is : (?!<a)(\bphp\b)(?!</a>)
$text = '<a href="http://www.php.net">php</a> is a web programming language . A popular php program is phpmyadmin. See http://www.php.net.'; $pattern = '/(?!<a)(\\bphp\\b)(?!<\/a>)/'; $replacement = '<strong>php</strong>'; echo preg_replace($pattern, $replacement, $text ); BOLD Text is results:
<a href="http://www.php.net">php</a> is a web programming language . A popular php program is phpmyadmin. See [[b]php[...]
As you can see, it does <a href="match">butdotmatch</a>
What can I do. Its not supposed to match the inside link. regex is wrecking my brain
I've modified the regexp to be case insensitive, and also not to catch text between <a>tags</a>.
Below is the final version for future users.
I've captured matches in a array, removed duplicates, and then do the replacing.
Once again, thanx alot.
<?php
// Text
$text = '<a href="http://www.php.net">php</a> is a web programming language . A popular pHp program is phpmyadmin. See [php.net.';...]// Word to search for
$lookfor = 'php';// regexp pattern
$pattern = '#(?!<.*?)(?!<a)(\\b'.$lookfor.'\\b)(?!<\/a>)(?![^<>]*?>)#i';
// (?!<a) (?!<\/a>) - prevents text in between <a></a>
// i (at the end) - case insensitive search// store all matches in an array
preg_match_all ($pattern, $text, $matches);// remove duplicate entries
$matches=array_unique($matches[0]);// Loop to Replace entries - case sensitive
foreach ($matches as $match)
{
$pattern = '#(?!<.*?)(?!<a)(\\b'.$match.'\\b)(?!<\/a>)(?![^<>]*?>)#';
$replacement = '<a href="http://linksomewhere"><span style="background: yellow">'.$match.'</span></a>';$text = preg_replace($pattern, $replacement, $text );
}echo $text;
?>