Forum Moderators: coopster
// Keyword Highlighting
$keys = str_replace(', ',',',$METkey);
$keys = explode(',',$keys);
$keylist = array();
foreach($keys as $key){
if(!$key ==''){ if(!in_array($key,$keylist)){
$pattern = "/(?!<.*?)(".preg_quote($key,'/').")(?![^<>]*?>)/si";
$replacement = "<strong>\\1</strong>";
$pagecontent = preg_replace($pattern,$replacement,$pagecontent);
$keylist[] = $key;
}}
}
At the moment it works a little too well... for example, if 'david' is in the keyword list, it will match 'david', 'davidson', 'davidian'
This screws things up as i get '<strong>david</strong>son', '<strong>david</strong>ian'
the function currently only effects matches between '>' and '<' symbols so that it can't mess up html which is stored with the content in the DB
I'm trying to figure out how to extend the code so that the word is only positively matched if:
1 - it is between '>' and '<'
2 - it is preceeded or followed by an immediate space, '.', ',', ':', ';', '?' or '-'
I received a LOT of assistance with the version above, I am still studying regex so whilst I follow the pattern, I'm struggling to extend it as I hope.
Any suggestions?
[edited by: eelixduppy at 12:12 am (utc) on April 21, 2008]
[edit reason] disabled smileys [/edit]
// Assuming ur keyword is 'cold'
$regExp = '/([\>]{1})([ \.,:;\?-]+[\S]*¦[\S]*[ \.,:;\?-]+)([\<]{1})/';
$str = '<a>cold</a>';
// Matches the following:
//$str = '<a> cold</a>';
//$str = '<a>cold?</a>';
//$str = '<a>:cold.</a>';
preg_match($regExp , $str, $aMatch);
print $aMatch[0];
I'm a leaf on the wind,
M. Cold