PHP Search Routine

A decent search function is a must for any serious site. There are countless ways to add search to your site, some very simple and some quite complicated. You can use a hosted service (like Google), a third-party script or you can design your own. If the latter interests you, I offer some sample code that I developed for this site, albeit in a slightly simplified form.

There are many features that can be included or omitted from a search script. Naturally, each feature adds complexity. The features that I think are most useful in a search function include:

  • An “advanced search” form displaying all the advanced search options
  • Boolean search choices (exact term, all terms, any terms)
  • Fields (and/or tables) to include in the search
  • Some sort of relevancy scoring to prioritize results
  • Sorting options (relevancy is the default, but others may be appropriate for your situation, such as title, posted date, etc.)
  • Multi-page results display and user option to choose number of results to display per page
  • Highlighted context text (several words before and after the searched for term(s) to provide some context to the result)

Below is the code (both some basic CSS styling and the core PHP code)

It is written to search a table called content with the two primary fields to search being title and content. Other relevant fields are ID and modified. Please note that, as with so many coding exercises, I put this together in pieces over time. As a result, I am sure there are many pieces of inefficient code. If you improve any of these, please let me know and I will update the script and provide appropriate acknowledgement.

As you can see, there are several main sections of code. Below I offer an overview of what each does.

prepare_results: this function takes the initial search results and assigns relevancy to each, highlights the search term(s), and selects context text. The relevancy algorithm is completely pulled from thin air on my part. My thinking is that I want an exact phrase match to have higher relevancy. I also want a match found in the title to have higher relevancy, all things equal, than a match in the content. You can pretty easily modify this algorithm to suit your needs. To get the context I break up all the content into words and assign them to an array. This allows for the context to contain only full words. You could simplify this greatly by just choosing a certain number of character before and after the found search term(s). Either way, the tricky thing to worry about it when you have multiple occurrences of a search term. If the occurrences are close in proximity, you could end up displaying the same partial text multiple times. My code corrects for this.

display_results: this function takes the final search results, sorts them and then displays an ordered list of the results. If appropriate, it creates a multi-page navigation at the bottom of the page and stores the search results (as well as the advanced search form options) in hidden variables to aid the functionality on the subsequent pages.

show advanced search box: this section of code displays the advanced search form, sets default option values if appropriate and also modifies the search term to remove any special characters which can be trouble for our database query.

Perform search or display multiple page as appropriate: this section of code performs the search if the search form was just submitted, but if we are using the multi-page results navigation there is no need to perform the search again as we store the results; we can just jump to the display_results section.

generate sql: this code uses the MySQL LIKE statement to do the searches. I have tried to use a text matching algorithm that loops through all table rows but have found that when the table size grows it becomes quite slow, especially since we are creating context display text, testing Boolean expressions, etc. The sql generated accounts for the Boolean or exact phrase nature of the search.

Finally, if you would rather not bother with implementing your own search feature, you might want to try a third party script. I haven’t used it myself, but I have heard that Sphider is a good option. It is a lightweight web spider and search engine written in PHP, using MySQL as its back end database. It is small, easy to set up and modify, and is used in thousands of websites across the world.

Like this content? Why not share it?
Share on FacebookTweet about this on TwitterGoogle+Share on LinkedInBuffer this pagePin on PinterestShare on Redditshare on TumblrShare on StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.