For the search feature on my site, the user searches for a term or a phrase then it gets processed and using the "LIKE" query to the mysql database - the relevant results are gotten. then they are shortened to 500 characters so snippets of the matching results are shown to the person searching. Only problem with this is that often the word which matched isn't in the 500chars (shortened) result and the user of the search engine may think its not working since the result isnt being displayed in the snippet.

Any (easy) ways around this?

A couple of thoughts:

  • Precede every entry with the keyword(s) that they entered, then the 500 char just to emphasize that there is a connection.
  • Scan each entry for the keyword / phrase and then show the result as "...keyword remaining 4xx characters" Thus the result in these cases will start with the keyword with whatever follows it.

I'm not quite sure what you meant there^^

Here's the code I use for limiting it to 500 chars, how would this code be changed to do what you just said:

/*limit how many chars to display*/
  if (strlen($content) > 500)
  {
  $position=500; 
  $post = substr($content,$position,1); // find what is the last character displaying by getting only last one character from the display message.
  while($post !=" "){ //until space is found
  $position++;
  $post = substr($content,$position,1); 
  }
  $post = substr($content,0,$position); 
  $content = $post . "..."; //only put ellipsis at the end if there's no dot there already
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.