Jump to content

How to highlight keywords???


pantinosm

Recommended Posts

Hello everyone. I have a search engine website where i have a textbox that the user input the search string and after pressing the search button i get the results. I want a way the results to be highlighting the words that were in the search textbox just like yahoo and google does. How can i do that?
Link to comment
Share on other sites

I now have it like this.
[code]<?php

if(strlen($row_rsfindwebsitesearch['Title'])<60)
{
$result = $row_rsfindwebsitesearch['Title'];
$result = str_replace($result,"<font style='background-color:yellow;'>$result",$result);
echo $result;

}

else echo trim(substr($row_rsfindwebsitesearch['Title'], 0, 60)).'  ...';


?>[/code]

The problem now is tht i highlight the whole sentence instead of the search string.
I dont know if it is appropriate but you can see the result at ibeba.com
Link to comment
Share on other sites

Well it looks to me like you are searching for the whole of the text within the variable result, and you are searching within the same variable, so it will replace the whole thing.

Here is a sample of how you should use it, im not using your variables because i dont know what they are

[code]
<?php
$text = "A test sentence";
$search_term = "test";
$output = str_replace($search_term,"<font style='background-color:yellow;'>$search_term</font>",$text);
echo $output;
?>
[/code]

So you look for the search term, replace it with the search term against a background in yellow, but you look for the search term within the whole text.

Link to comment
Share on other sites

or

[CODE]
<?php

function highlight($str, $rep) {
        $begin = '<span style="background-color:yellow">';
        $end = '</span>';
        $color = $begin.$rep.$end;
        return eregi_replace($rep, $color, $str);
}


$str = 'some text, some text, some text, some text, some text, some text';
$strRep = 'some';

$str = highlight($str, $strRep);
echo $str;

?>
[/CODE]
Link to comment
Share on other sites

Thank you very much GingerRobot it worked. But it only works with one word as the search term. Is the a way to highlight any word in the search term. E.g if the search term is "test query" it should highlight query individually and test individually.

Can it be done?
Link to comment
Share on other sites

do something like this if you want to highlight them individually:

[CODE]<?php

function highlight($str) {
$tmp = '';

foreach(explode(' ', $str) as $word)
$tmp .= "<span style=\"background-color:yellow\">$word</span> ";

return trim($tmp);

}

$query = "test query";
$string = "hello there, this is a test query";

echo preg_replace("/$query/ise", 'highlight("\\0")', $string);

?>[/CODE]
Link to comment
Share on other sites

[quote]why you call the highlight function with highlight("\\0")?[/quote]

\\0 is a backreference and holds the matched keyword(s)

[quote]Because now the keywords in the result instead of highlight they get replaced with "highlight ("keywords")" string!!!![/quote]

Show me your code.
Link to comment
Share on other sites

The code is [code]
<?php
function highlight($str) {
$tmp = '';

foreach(explode(' ', $str) as $word)
$tmp .= "<span style=\"background-color:pink\">$word</span> ";

return trim($tmp);

}
?>

<?php
$result = $row_rsfindwebsitesearch['Description'];
$searchterm = $_GET['txtsearch'];

echo preg_replace("/$searchterm/is", 'highlight("\\0")',  $result);

  ?>
[/code]

What am i doing wrong?
Link to comment
Share on other sites

  • 4 weeks later...
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.