pantinosm Posted January 8, 2007 Share Posted January 8, 2007 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 look up strpos(), str_replace(). Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 9, 2007 Author Share Posted January 9, 2007 can you be a little more specific? Quote Link to comment Share on other sites More sharing options...
craygo Posted January 9, 2007 Share Posted January 9, 2007 Not sure how you are searching, but you could always look into using css files. if the word is found use a different style. A little code might help if you need help.Ray Quote Link to comment Share on other sites More sharing options...
marcus Posted January 9, 2007 Share Posted January 9, 2007 With str_replace(); you could do:[code=php:0]$result = $_POST[result];$result = str_replace($result,"<font style='background-color:yellow;'>$result",$result);echo $result;[/code] Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 12, 2007 Author Share Posted January 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
Eddyon Posted January 12, 2007 Share Posted January 12, 2007 have you tried putting </font> after the result? Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 12, 2007 Author Share Posted January 12, 2007 Yes like this $result = str_replace($result,"<font style='background-color:yellow;'>$result</font>",$result);and same result :( Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 13, 2007 Author Share Posted January 13, 2007 It highlights all the line because it is the result. I understand this. But how can i highlight only the search words???Please help. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 13, 2007 Share Posted January 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
tuuga Posted January 13, 2007 Share Posted January 13, 2007 or[CODE]<?phpfunction 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] Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 13, 2007 Share Posted January 13, 2007 Yes, you could always set up a function. However, you shouldn't use ereg_replace or preg_replace for this kind of thing, str_replace(or str_ireplace if you want it to be case insensitive) is faster. The other two should be used if you want to use regular expressions. Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 13, 2007 Author Share Posted January 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
Nicklas Posted January 13, 2007 Share Posted January 13, 2007 http://www.phpfreaks.com/forums/index.php/topic,115226.msg469086.html#msg469086 Quote Link to comment Share on other sites More sharing options...
Nicklas Posted January 13, 2007 Share Posted January 13, 2007 do something like this if you want to highlight them individually:[CODE]<?phpfunction 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] Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 14, 2007 Author Share Posted January 14, 2007 Thank you Nicklas but where is $tmp used in preg_replace??I didn't understand. Quote Link to comment Share on other sites More sharing options...
Nicklas Posted January 14, 2007 Share Posted January 14, 2007 the preg_replace call the function highlight() and takes the match, split it and then surround each word with a <span> tag. Then the string is put back together via the $tmp variable and return it to preg_replace(). Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 14, 2007 Author Share Posted January 14, 2007 Yes but why you call the highlight function with highlight("\\0")? Where do you send the string? Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 14, 2007 Author Share Posted January 14, 2007 Because now the keywords in the result instead of highlight they get replaced with "highlight ("keywords")" string!!!! Quote Link to comment Share on other sites More sharing options...
Nicklas Posted January 15, 2007 Share Posted January 15, 2007 [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. Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 15, 2007 Author Share Posted January 15, 2007 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? Quote Link to comment Share on other sites More sharing options...
Nicklas Posted January 16, 2007 Share Posted January 16, 2007 The [b]e[b] modifier is missing in preg_replace()change to this:[code=php:0]echo preg_replace("/$searchterm/ise", 'highlight("\\0")', $result);[/code] Quote Link to comment Share on other sites More sharing options...
pantinosm Posted January 17, 2007 Author Share Posted January 17, 2007 Thank you that worked. The only thing now is that i want to make it case in-sensitive. Is there any way of doing that? Quote Link to comment Share on other sites More sharing options...
pantinosm Posted February 11, 2007 Author Share Posted February 11, 2007 The problem is that i get the highlighted resultls in 70% right. Some keywords are not highlighted all the time. I can see the same keyword somewhere highlighted and somewhere not. Is this a bug or something? Quote Link to comment 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.