Forums

PHP

This topic is locked

Highlighting search terms in quotes.

Posted 29 Aug 2006 17:44:15
1
has voted
29 Aug 2006 17:44:15 Iain MacDonald posted:
Hope someone can help with this - I have a function that I'm using to highlight search terms in the results page :


<?php function highlightResult($keyword,$result) {
  if (get_magic_quotes_gpc()) {
    $replace = stripslashes($keyword);
    }
  else {
    $replace = $keyword;
    }
  $replaceArr = explode(" ",$replace);
  for ( $i = 0; $i < sizeof($replaceArr); $i++ ) {
   $result = preg_replace("/{$replaceArr[$i]}/i","<b><font color=#FF0000>{$replaceArr[$i]}</font></b>",$result);
  }
  $highlighted = $result;
  return $highlighted;
  }
?>


It works for search words, but not for words combined into phrases using inverted commas....

Does anyone know how to amend it to work for search phrases in quotes too?

Cheers

Replies

Replied 29 Aug 2006 18:28:54
29 Aug 2006 18:28:54 Roddy Dairion replied:
Am sorry but what do you mean not for words combine into phrase using inverted commas?
Replied 29 Aug 2006 18:44:23
29 Aug 2006 18:44:23 Iain MacDonald replied:
Basically the search code I'm using uses inverted commas to group individual words as a phrase.

eg

If you search on : technical knowledge

It will return any results that contain technical OR knowledge. (and are highlighted correctly)

If you search on : technical, knowledge

It will return any results that contain technical AND knowledge. (and are highlighted correctly)

If you search on : "technical knowledge"

It will return any results that contain the exact phrase technical knowledge - and in this case although the results are correct, the phrase isn't highlighting.

Replied 29 Aug 2006 18:58:05
29 Aug 2006 18:58:05 Roddy Dairion replied:
It could be that your search result is working but when it comes to highlighting instead of reading technical knowledge its reading "technical knowledge", in which case it will not highlight at its not a matching phrase because of the inverted commas, so what you need is to break the phrase so that it remove the inverted commas and make it read the words or phrase inside.
Replied 29 Aug 2006 19:51:22
29 Aug 2006 19:51:22 Iain MacDonald replied:
That sounds about right - if you search on a phrase of more thanb two words, then the words in the middle highlight correctly, but not the first and last, as they presumably include the quote marks.

I'm just not sure of the syntax for the code to do that, and exactly where to fit it in....

Replied 30 Aug 2006 11:57:42
30 Aug 2006 11:57:42 Roddy Dairion replied:
what i can't understand is why do u need to use double quotes. it works ok without it. why not just remove them?
Replied 30 Aug 2006 12:14:35
30 Aug 2006 12:14:35 Iain MacDonald replied:
I think it only does if you want to search on a single exact phrase.

Up until now I've just used Tom Muck's excellent extension that allows you to search on any words, all words, or exact phrase.

However, for a project I'm working on, I have a requirement to be able to search on multiple phrases, eg

"technical knowledge", "aviation engineer"

So without the quotes, I only have the ability to search on

technical AND knowledge AND aviation AND engineer

or

technical OR knowledge OR aviation OR engineer

or the exact phrase :

technical knowledge aviation engineer

But unless I'm missing something, how would I limit the search to instances of

technical knowledge AND aviation engineer?

Anyhoo - I got some code that seems to do the trick, mostly at least - there still seem to be issues with case sensitivity, and also how to pass through the search string to the details page....see the thread on session variables.

code :

<?php function highlightResult($keyword,$result) { 
if (get_magic_quotes_gpc()) { 
$replace = stripslashes($keyword); 
} 
else { 
$replace = $keyword; 
} 
$replace = preg_replace("/\"|\,/i","",$replace); 
$replace = preg_replace("/ {2,}/i"," ",$replace); 
$replaceArr = explode(" ",$replace); 
for ( $i = 0; $i < sizeof($replaceArr); $i++ ) { 
$result = preg_replace("/{$replaceArr[$i]}/i","<b>{$replaceArr[$i]}</b>",$result); 
} 
$highlighted = $result; 
return $highlighted; 
} 
?>

Reply to this topic