Jump to content

Recommended Posts

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 : )

Link to comment
https://forums.phpfreaks.com/topic/82562-highlight-keyword-case-insensitive/
Share on other sites

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

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. :)

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.