Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Highlighting search terms in quotes

Enthusiast ,
Aug 29, 2006 Aug 29, 2006

Copy link to clipboard

Copied

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

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.

I presume when it comes to highlighting instead of reading technical knowledge its reading "technical knowledge", in which case it's not highlighting at its not a matching phrase because of the inverted commas, so what I need is to somehow break the phrase so that it remove the inverted commas and make it read the words or phrase inside...

If anyone could help amend the code above to do this that would be hugely appreciated.

Cheers,
Iain
TOPICS
Server side applications

Views

293
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Aug 30, 2006 Aug 30, 2006
If it ever comes in handy for anyone else, the code that did it was :

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

Votes

Translate
Enthusiast ,
Aug 30, 2006 Aug 30, 2006

Copy link to clipboard

Copied

LATEST
If it ever comes in handy for anyone else, the code that did it was :

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines