The Little Guy Posted July 13, 2007 Share Posted July 13, 2007 Here is my code: <?php function boldText($string, $array){ $string = strip_tags($string); return preg_replace('~\b('.implode('|', $array).'[a-zA-Z]{0,45})\b(?![^<]*[>])~is', '<strong>$0</strong>', $string ); } ?> This will bold all words that contain any word in the array, so if the string is my superman cat and I search for super I would like the word super to be bold in superman so, when the results come back from my search, I would like it to display like this: my superman cat Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/ Share on other sites More sharing options...
Barand Posted July 13, 2007 Share Posted July 13, 2007 try <?php function boldText ($str, $srch) { $res = ''; $rplc = array(); foreach ($srch as $s) { $rplc[] = "<strong>$s</strong>"; } return str_replace ($srch,$rplc,$str); } $str = 'my superman cat'; echo boldText ($str, array('super', 'at')); ?> Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297580 Share on other sites More sharing options...
Lumio Posted July 13, 2007 Share Posted July 13, 2007 Or that <?php global $find; $fing = 'test'; function boldText($subject) { $s = preg_quote($find); $subject = preg_replace("/($s)/i", '<strong>$1</strong>', $subject); return $subject; } $array = array_map('boldText', $array); ?> Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297587 Share on other sites More sharing options...
The Little Guy Posted July 13, 2007 Author Share Posted July 13, 2007 neither of those work... Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297624 Share on other sites More sharing options...
Glyde Posted July 13, 2007 Share Posted July 13, 2007 Barand's should work fine. Remeber PHP is a case sensitive language...in Barand's example, change str_replace to str_ireplace if you need case insensitivity Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297635 Share on other sites More sharing options...
The Little Guy Posted July 13, 2007 Author Share Posted July 13, 2007 I just realized that, but then using str_ireplace, it will lowercase all the upercase letters. Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297648 Share on other sites More sharing options...
The Little Guy Posted July 13, 2007 Author Share Posted July 13, 2007 well... how can i make it not lowercase the letters? Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297700 Share on other sites More sharing options...
Glyde Posted July 13, 2007 Share Posted July 13, 2007 At that point...probably regular expressions...it's the best way I can think of at least. Try: <?php $myWords = array('super', 'at'); function boldText($arrWords, $strSubject) { if (!is_array($arrWords)) return; foreach ($arrWords as $strWord) { $strSubject = preg_replace('@(' . preg_quote($strWord, '@') . ')@i', "<b>\\1</b>", $strSubject); } return $strSubject; } print boldText($myWords, 'my SuPeRman cat'); ?> Tested under PHP 5.2.1 and works...in a few minutes I'll bring up reports as to how long it takes. Edit: Got the "report" Speed results: My function versus Barand's function: me@server:/path$ php -f boldText.php Running 10000 cycles of function boldText_Glyde() Cycles finished in 0.18140411377 seconds Running 10000 cycles of function boldText_Barand() Cycles finished in 0.121095895767 seconds You either deal with the inconvenience of case-changing and go for the faster one, or take a slightly longer amount of time and have it output the way you want. Either way, those are the results. Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297725 Share on other sites More sharing options...
The Little Guy Posted July 14, 2007 Author Share Posted July 14, 2007 Perfect!!! Thank You Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-297948 Share on other sites More sharing options...
Lumio Posted July 14, 2007 Share Posted July 14, 2007 Mine works also... I just forgot 2 things: <?php $array = array('this is a TEST', 'oh test it'); global $find; $find = 'test'; function boldText($subject) { global $find; $s = preg_quote($find); $subject = preg_replace("/($s)/i", '<strong>$1</strong>', $subject); return $subject; } $array = array_map('boldText', $array); print_r($array); ?> Link to comment https://forums.phpfreaks.com/topic/59850-solved-bold-string-from-array-of-keywords/#findComment-298014 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.