jamesjmann Posted June 9, 2011 Share Posted June 9, 2011 I have this so far, but all it does is hightlight ONE of the search keywords out of the item retrieved from the database... <?php //Display replies if ($count_4 > 0) { while ($row_4 = mysql_fetch_array($result_4)) { $keywords = explode (" ", $string); foreach ($keywords as $field => $value) { $new_row = $row_4["reply"]; $newest_row = preg_replace ("/$value/", "<strong>$value</strong>", "$new_row"); } echo $newest_row; } } ?> Basically, it goes through every item to be fetched; explodes the string they searched into an array (only if spaces were used), then goes through each individual topic and finds and replaces each keyword with the same keyword in bold. For some reason it only "bolds" one of the keywords out of the array. Anyone know how to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/ Share on other sites More sharing options...
requinix Posted June 9, 2011 Share Posted June 9, 2011 Unless $value is an actual regular expression, use str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227686 Share on other sites More sharing options...
The Little Guy Posted June 9, 2011 Share Posted June 9, 2011 You can do this, mine just makes the words bold, but you can style it to do what ever you want: <?php $full_text = "Do you like to eat pizza?"; $search_string = "I like pizza"; $words = explode(' ', $search_string); $new_string = preg_replace("/(".implode("|", $words).")/i", "<b>$1</b>", $full_text); echo $new_string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227706 Share on other sites More sharing options...
jamesjmann Posted June 10, 2011 Author Share Posted June 10, 2011 You can do this, mine just makes the words bold, but you can style it to do what ever you want: <?php $full_text = "Do you like to eat pizza?"; $search_string = "I like pizza"; $words = explode(' ', $search_string); $new_string = preg_replace("/(".implode("|", $words).")/i", "<b>$1</b>", $full_text); echo $new_string; ?> Mine just makes the words bold too, but what is the difference between yours and mine? and don't forget, im using mysql to get data and storing it into variables, unlike you where your declaring a static variable. i think there's a huge difference between applying this method to a static variable, and applying it to a dynamic variable that runs through a loop. Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227773 Share on other sites More sharing options...
revraz Posted June 10, 2011 Share Posted June 10, 2011 Doesn't matter how you populate the variable. Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227775 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2011 Share Posted June 10, 2011 i think there's a huge difference between applying this method to a static variable There's absolutely no difference. The point of using a variable at all is so you can write code that does something to the contents of that variable. It does not matter if that variable gets set from a literal string assignment in some code to demonstrate how to do something or if that variable gets set by fetching a value from a database query. <?php //Display replies if ($count_4 > 0) { $keywords = str_replace(" ", "|", $string); while ($row_4 = mysql_fetch_array($result_4)) { $new_string = preg_replace("/($keywords)/i", "<b>$1</b>", $row_4["reply"]); echo $new_string; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227776 Share on other sites More sharing options...
jamesjmann Posted June 10, 2011 Author Share Posted June 10, 2011 i think there's a huge difference between applying this method to a static variable There's absolutely no difference. The point of using a variable at all is so you can write code that does something to the contents of that variable. It does not matter if that variable gets set from a literal string assignment in some code to demonstrate how to do something or if that variable gets set by fetching a value from a database query. <?php //Display replies if ($count_4 > 0) { $keywords = str_replace(" ", "|", $string); while ($row_4 = mysql_fetch_array($result_4)) { $new_string = preg_replace("/($keywords)/i", "<b>$1</b>", $row_4["reply"]); echo $new_string; } } ?> Can you explain what the above code does? what's with the "|" character, and where did you get the random "i" in the pattern and the $1 variable? Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227777 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2011 Share Posted June 10, 2011 The following quotes are from the pcre and preg_replace sections of the php manual - http://us2.php.net/manual/en/pcre.pattern.php and http://us2.php.net/manual/en/function.preg-replace.php Vertical | - Alternation Vertical bar characters are used to separate alternative patterns. For example, the pattern gilbert|sullivan matches either "gilbert" or "sullivan". Any number of alternatives may appear, and an empty alternative is permitted (matching the empty string). Not so random i - i (PCRE_CASELESS) If this modifier is set, letters in the pattern match both upper and lower case letters. $1 - replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern The | forms a regular expression to match any of your search keywords - keyword1|keyword2|keyword3 The i causes the preg_replace to match both upper and lower case so that for example keyword1 will match KeYwOrD1 in the text. The $1 is a variable/reference to the actual value (which ever keyword out of the possible choices) that was actually matched so you can let the preg_ statement do the work instead of you needing to cycle through each choice, one at a time. Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227781 Share on other sites More sharing options...
jamesjmann Posted June 10, 2011 Author Share Posted June 10, 2011 The following quotes are from the pcre and preg_replace sections of the php manual - http://us2.php.net/manual/en/pcre.pattern.php and http://us2.php.net/manual/en/function.preg-replace.php Vertical | - Alternation Vertical bar characters are used to separate alternative patterns. For example, the pattern gilbert|sullivan matches either "gilbert" or "sullivan". Any number of alternatives may appear, and an empty alternative is permitted (matching the empty string). Not so random i - i (PCRE_CASELESS) If this modifier is set, letters in the pattern match both upper and lower case letters. $1 - replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern The | forms a regular expression to match any of your search keywords - keyword1|keyword2|keyword3 The i causes the preg_replace to match both upper and lower case so that for example keyword1 will match KeYwOrD1 in the text. The $1 is a variable/reference to the actual value (which ever keyword out of the possible choices) that was actually matched so you can let the preg_ statement do the work instead of you needing to cycle through each choice, one at a time. It works and I understand it now. Spose I shoulda read the manual before sitting down for five hours trying to work with fussy loops lol. Thanks =) Quote Link to comment https://forums.phpfreaks.com/topic/238907-highlighting-search-keywords-in-search-results/#findComment-1227786 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.