homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

preg replace to find keywords

 1:54 pm on Apr 17, 2008 (gmt 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)

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)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved