Jump to content

Highlighting Search Keywords In Search Results


jamesjmann

Recommended Posts

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?

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}
}
?>

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.