SchweppesAle Posted June 17, 2009 Share Posted June 17, 2009 hi, the following code replaces all keywords with a linked keyword. However, I would like to use REGEX in order to avoid the manipulation of keywords when they fall within links. ex: if the keyword is orange <br>orange</br> would become <br><a href = "somelink">orange</a></br> however I'd like to avoid the following original string: <a href = "http://somedomain.com/orange">yadayada</a> string after preg_replace: <a href = "http://somedomain.com/<a href = "somelink">orange</a>">yadayada</a> code: global $mainframe; $content = $article -> text; $current = JURI::current(); $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 = "../plugins/content/keyword.php?destination='.urlencode($Links[$i]).'&location='.urlencode($current).'&keyword='.$words[$i].'">'.$words[$i].'</a>'; } 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; Link to comment https://forums.phpfreaks.com/topic/162633-solved-using-regex-to-avoid-preg_replace-of-links/ Share on other sites More sharing options...
SchweppesAle Posted June 17, 2009 Author Share Posted June 17, 2009 actually, I think it's the slash in the urls which is throwing the current pattern off. reviews/payroll-relief-from-accountantsworld.html payroll would be replaced in the above string, maybe it's due to the slash preceding the keyword(payroll)?? Link to comment https://forums.phpfreaks.com/topic/162633-solved-using-regex-to-avoid-preg_replace-of-links/#findComment-858352 Share on other sites More sharing options...
SchweppesAle Posted June 17, 2009 Author Share Posted June 17, 2009 nvm. I just tested it, the forward slash is definetelly the culprit. How can I keep word boundary from ignoring a forward slash / within urls? Link to comment https://forums.phpfreaks.com/topic/162633-solved-using-regex-to-avoid-preg_replace-of-links/#findComment-858395 Share on other sites More sharing options...
thebadbad Posted June 17, 2009 Share Posted June 17, 2009 Nothing to do with the slash. But I got the solution for you. I've utilized a negative lookahead: for($i = 0; $i < $number; $i++) { $words[$i] = '/' . preg_quote($words[$i], '/') . '(?![^<]*?>)/'; /*$words[$i] = str_replace(' ', '', $words[$i]);*/ } It searches for the keyword, and when found, checks the following characters. If any other character than < is found 0 or more times, immediately followed by a >, the whole thing fails to match (= the keyword is inside a HTML tag, i.e. between < and >). But if a < is found before a > (to say it loosely), the pattern does match, resulting in a replaced keyword. Basically, keywords found between a set of <> aren't replaced. Hope that's what you're looking for Link to comment https://forums.phpfreaks.com/topic/162633-solved-using-regex-to-avoid-preg_replace-of-links/#findComment-858423 Share on other sites More sharing options...
SchweppesAle Posted June 18, 2009 Author Share Posted June 18, 2009 worked perfectly! Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/162633-solved-using-regex-to-avoid-preg_replace-of-links/#findComment-858534 Share on other sites More sharing options...
MadTechie Posted June 18, 2009 Share Posted June 18, 2009 can you click topic solved please Link to comment https://forums.phpfreaks.com/topic/162633-solved-using-regex-to-avoid-preg_replace-of-links/#findComment-858541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.