SchweppesAle Posted May 29, 2009 Share Posted May 29, 2009 hi, my problem is that the following code isn't pulling individual words but rather any set of matching characters. How can I correct the following $content = $article -> text; $keywords = $this -> params->def('keywords'); $words = explode(",", $keywords); $keywordLinks = $this -> params->def('links'); $Links = explode(",", $keywordLinks); $number = count($words); for($x = 0; $x <= count($words); $x++) { if(preg_match_all("/\b".$words[$x]."\b/i", $content, $matches)) { foreach($matches[0] as $change) { $content = str_replace($change, str_replace($words[$x], '<a href = "'.$Links[$x].'">'.$words[$x].'</a>', $change), $content); } } } $article -> text = $content; Quote Link to comment https://forums.phpfreaks.com/topic/160111-solved-setting-word-boundaries-with-regex/ Share on other sites More sharing options...
SchweppesAle Posted May 29, 2009 Author Share Posted May 29, 2009 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/160111-solved-setting-word-boundaries-with-regex/#findComment-845130 Share on other sites More sharing options...
DarkSuperHero Posted May 29, 2009 Share Posted May 29, 2009 $content = $article -> text; $keywords = $this -> params->def('keywords'); $words = explode(",", $keywords); $keywordLinks = $this -> params->def('links'); $Links = explode(",", $keywordLinks); $number = count($words); for($x = 0; $x <= count($words); $x++) { if(preg_match_all("/(".$words[$x].")+/i", $content, $matches)) //changes it to parenthesis and removed /b's { foreach($matches[0] as $change) { $content = str_replace($change, str_replace($words[$x], '<a href = "'.$Links[$x].'">'.$words[$x].'</a>', $change), $content); } } } $article -> text = $content; maybe? Quote Link to comment https://forums.phpfreaks.com/topic/160111-solved-setting-word-boundaries-with-regex/#findComment-845138 Share on other sites More sharing options...
SchweppesAle Posted May 29, 2009 Author Share Posted May 29, 2009 still no good. I'll actually test the pattern by using the word "black" then run it through a string with the following output: blackwhitered >black< blackblackblack black. Every one of them is being picked up though. Am I doing this wrong? :/ Quote Link to comment https://forums.phpfreaks.com/topic/160111-solved-setting-word-boundaries-with-regex/#findComment-845233 Share on other sites More sharing options...
SchweppesAle Posted June 1, 2009 Author Share Posted June 1, 2009 was actually very simple public function onPrepareContent(&$article, &$params, $limitstart) { global $mainframe; $content = $article -> text; $keywords = $this -> params->def('keywords'); $words = explode(",", $keywords); $keywordLinks = $this -> params->def('links'); $Links = explode(",", $keywordLinks); $number = count($words); for($i = 0; $i < $number; $i++) { $Links[$i] = '<a href = "'.$Links[$i].'">'.$words[$i].'</a>'; /*$Links[$i] = str_replace(' ', '', $Links[$i]);*/ } for($i = 0; $i < $number; $i++) { $words[$i] = '/\b'.$words[$i].'\b/'; $words[$i] = str_replace(' ', '', $words[$i]); } $content = preg_replace($words, $Links, $content); $article -> text = $content; preg_replace, done. lol Quote Link to comment https://forums.phpfreaks.com/topic/160111-solved-setting-word-boundaries-with-regex/#findComment-847268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.