Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg replace to find keywords

1:54 pm on Apr 17, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 14, 2007
votes: 0

Currently have the following filter to check content extracted from my DB against a list of keywords - when matches are found it automatically adds <strong></strong> tags around the word.

// 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]

3:01 pm on Apr 17, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 26, 2004
votes: 0

Ok. First off, I'm not the best at RegExps. Hopefully, though, this will steer you in the right direction!

// 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

2:57 pm on Apr 18, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 14, 2007
votes: 0

thanks for suggestion I'll have a go and report back

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members