The Little Guy Posted October 23, 2008 Share Posted October 23, 2008 I have this regex function to bold words ($words is an array): function boldWords($words, $string){ return preg_replace('~('.implode('|', $words).')~i','<strong>$0</strong>',$string); } sometimes there is a "wildcard" input value (asterisk). The current above code takes something like: new* and bolds: "newest thing ever" I would like it to bold: "newest thing ever" so basically bold the whole word not just part of the word. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/ Share on other sites More sharing options...
The Little Guy Posted October 23, 2008 Author Share Posted October 23, 2008 OK, I have modified my code, it still needs some help with my question: function boldWords($words, $string){ return preg_replace('~('.implode('|', $words).')(.*?)(\s|\Z|)~i','<strong>$0</strong>',$string); } Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-672463 Share on other sites More sharing options...
Orio Posted October 23, 2008 Share Posted October 23, 2008 This worked for me: <?php echo boldWords(array("one", "t*"), "One is before two and three is too."); function boldWords($words, $string) { foreach($words as $k => $w) { $w = '('.str_replace('*', '.*?)', $w, $count); if(!$count) $w .= ')'; $words[$k] = $w; } $regex = "~".implode('|', $words)."\b~i"; return preg_replace($regex ,'<strong>$0</strong>',$string); } ?> Enjoy Orio. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-672726 Share on other sites More sharing options...
effigy Posted October 23, 2008 Share Posted October 23, 2008 Sanitize the words with preg_quote. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-672762 Share on other sites More sharing options...
Orio Posted October 23, 2008 Share Posted October 23, 2008 Sanitize the words with preg_quote. Good point. But that will require a small fix: <?php echo boldWords(array("one", "t*"), "One is before two and three is too."); function boldWords($words, $string) { foreach($words as $k => $w) { $w = '('.str_replace('\*', '.*?)', preg_quote($w), $count); if(!$count) $w .= ')'; $words[$k] = $w; } $regex = "~".implode('|', $words)."\b~i"; return preg_replace($regex ,'<strong>$0</strong>',$string); } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-672954 Share on other sites More sharing options...
The Little Guy Posted October 23, 2008 Author Share Posted October 23, 2008 that didn't work. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-673139 Share on other sites More sharing options...
The Little Guy Posted October 23, 2008 Author Share Posted October 23, 2008 if you do echo boldWords(array("one", "t*"), "One is before two and three is too."); // works echo boldWords(array("t*", "one"), "One is before two and three is too."); // doesn't work Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-673148 Share on other sites More sharing options...
Orio Posted October 23, 2008 Share Posted October 23, 2008 Just added some brackets for the ORs: <?php echo boldWords(array("one", "t*"), "One is before two and three is too."); function boldWords($words, $string) { foreach($words as $k => $w) { $w = '('.str_replace('\*', '.*?)', preg_quote($w), $count); if(!$count) $w .= ')'; $words[$k] = $w; } $regex = "~(".implode('|', $words).")\b~i"; return preg_replace($regex ,'<strong>$0</strong>',$string); } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-673182 Share on other sites More sharing options...
The Little Guy Posted October 23, 2008 Author Share Posted October 23, 2008 Looks Good! Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-673209 Share on other sites More sharing options...
Orio Posted October 24, 2008 Share Posted October 24, 2008 You're welcome....... Orio. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-673595 Share on other sites More sharing options...
The Little Guy Posted October 24, 2008 Author Share Posted October 24, 2008 You're welcome....... Orio. Thank You Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-673876 Share on other sites More sharing options...
The Little Guy Posted October 30, 2008 Author Share Posted October 30, 2008 I realized, as I was doing searches.... that if I searched for: a** I get this error: Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 11 in /home/.marble/ryannaddy/dudeel.com/incl/functions.php on line 67 Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 11 in /home/.marble/ryannaddy/dudeel.com/incl/functions.php on line 67 Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-678095 Share on other sites More sharing options...
ddrudik Posted October 30, 2008 Share Posted October 30, 2008 this should cover the wildcards * and ?: <?php echo boldWords(array("o?e", "a**", "t??", "b*e"), "One is before two and three is too."); function boldWords($words, $string) { $regex = "~\b(?:".preg_replace('/(?:\\\\\*)+/','.*?',str_replace('\?','.',implode('|', array_map('preg_quote',$words)))).")\b~i"; return preg_replace($regex ,'<strong>$0</strong>',$string); } ?> Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-678153 Share on other sites More sharing options...
Orio Posted October 31, 2008 Share Posted October 31, 2008 I realized, as I was doing searches.... that if I searched for: a** Why would you input something like that? It's like using in SQL "... LIKE 'something%%' ...". I can't see what you're trying to achieve using double wildcards and that's why the function is not build for such input. Orio. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-679140 Share on other sites More sharing options...
discomatt Posted October 31, 2008 Share Posted October 31, 2008 I think the issue is the end user typing in double stars. Your best bet is to match \*++ in the regex that does the conversion. Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-679374 Share on other sites More sharing options...
The Little Guy Posted October 31, 2008 Author Share Posted October 31, 2008 I realized, as I was doing searches.... that if I searched for: a** Why would you input something like that? It's like using in SQL "... LIKE 'something%%' ...". I can't see what you're trying to achieve using double wildcards and that's why the function is not build for such input. Orio. like discomatt said, a star is a wildcard that users can enter into a text field. you can try it here: http://dudeel.com/#searchVideo Link to comment https://forums.phpfreaks.com/topic/129682-bold-words-from-array/#findComment-679437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.