PrMa Posted December 20, 2007 Share Posted December 20, 2007 Hey! This is the code I use to display search results. echo "<a target=_self href=\"".$r['url']."\">".$r['title']."</a><br>...".substr($r['conts'], strpos($r['conts'], $_GET[s])-50, 100); Now to the questions. 1. How can I make the keyword highlighted in the search result page? 2. How can I make the keyword case insensitive? For example, if I search for code, and the searched text consists of Code, I will find a result, but it won't be displayed. For that to happen, I'll have to type in Code. So, if someone typed in coDe, then I want the result showing to be coDe. Dunno if this makes any sense, but I want the keyword to be displayed exactly as it was typed in, no matter in wich form it is written in, in the database. Thx : ) Quote Link to comment https://forums.phpfreaks.com/topic/82562-highlight-keyword-case-insensitive/ Share on other sites More sharing options...
mbeals Posted December 20, 2007 Share Posted December 20, 2007 First off, how are you searching now? The stripos() function is case insensitive. Ii searches a target string for a sub string and returns the position where the substring starts. You could easily do something like: $keyword = 'whatever'; for($i=0;$i<count($possible);$i++){ if(stripos($possible[$i],$keyword)) { echo "<a target=_self href=\"".$r['url']."\">".$r['title']."</a><br>...".substr($r['conts'], strpos($r['conts'], $_GET[s])-50, 100); } } or if you are searching a mysql database, use 'like', as it is case insensitive as well. so basically if stripos returns something (it returns false if the search fails), then echo the result To highlight the key word, add <style type="text/css"> <!-- #highlight { background-color: #00FF00; } --> </style> to the head of the results page and then dynamically encase the keyword with <span id='highlight'> </span> when you echo the result Quote Link to comment https://forums.phpfreaks.com/topic/82562-highlight-keyword-case-insensitive/#findComment-419824 Share on other sites More sharing options...
PrMa Posted December 20, 2007 Author Share Posted December 20, 2007 Thx! I solved the matter about case insensitivity. The other problem (keyword-highlighting) is sadly a thougher nut to crack. In fact I knew the principle of how to do it before I posted, but unfortunately I can't get it to work. I have tried to place the encasing in every single spot, but it wont just work. So I would like if someone better than me, put it in the right place of my code. Quote Link to comment https://forums.phpfreaks.com/topic/82562-highlight-keyword-case-insensitive/#findComment-419901 Share on other sites More sharing options...
mbeals Posted December 21, 2007 Share Posted December 21, 2007 can you post more of your code.... specifically the echo lines where are attempting to highlight? Quote Link to comment https://forums.phpfreaks.com/topic/82562-highlight-keyword-case-insensitive/#findComment-420470 Share on other sites More sharing options...
Zane Posted December 21, 2007 Share Posted December 21, 2007 you could always put it in bold or do that and change the font color just echo out the HTML like you did for the link itself Quote Link to comment https://forums.phpfreaks.com/topic/82562-highlight-keyword-case-insensitive/#findComment-420476 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.