Removing "The" from searches

Posted on 2005-04-27
Last Modified: 2008-03-06

I need to find a way to remove the english word "The" from titles when searching.
My titles are reversed like this: "Rock, The", "Shield, The" and so on. This due to
drop down menus and things like that.

If a visitor searches for "The Rock", they will not get any results.
If a visitor searches for "Rock, The", they will get one hit.

The problem is that no one is searching for "Rock, The".

I have tried to swap the the words like this:
if(substr_count($strSearchWord, 'the ') == 1){
      $strSearchWord = trim(str_replace('the ','',$strSearchWord));
      $strSearchWord = substr_replace($strSearchWord, ', the', strlen($strSearchWord), 0);

This works, almost. If a user is searching for "King of the Hill", it does not return any results due to the "The" in the middle of the search word.

Hope you understand what im after here.
The SQL statment is standard "WHERE title LIKE '%$strSearchWord%';"

Thanks in advance,
best regards
Question by:Aknot
    LVL 6

    Accepted Solution

    Hi Aknot,

    There are several solutions for your problem.
    Probably the most effective will be using FULLTEXT search (
    I really recommend trying this.

    If you simply want to remove 'the' from the beginning of string use regular expressions

    $strSearchWord = preg_replace('/^the */i', '', $strSearchWord);

    LVL 3

    Assisted Solution

    if you are going for a fulltext search, before your move, read the following.

    or otherwise try the following.
    // filtering of words
    $filter = array ("the","of","a","in");// etc

    $search = "King of the Hill";

    $trimmed = trim($search);
    $spilit = explode(" ",$trimmed);

    for ($i=0; $i<=sizeof($spilit); $i++) {
          if (!in_array($spilit[$i],$filter)) {
                $searchstr .= $spilit[$i]." ";

    $search = trim($searchstr);

    $spilitagain = explode(" ",$search);

    $sql = "WHERE ";

    foreach ($spilitagain as $querysearch) {
          $sql .= "title like '%$querysearch%' OR ";
    $sql = substr($sql,0,-3);

    //if you want order some columns
    $sql .=" ORDER BY id";

    hope this helps
    LVL 6

    Expert Comment


    not "OR", but "AND", otherwise you will find all movies with just "King" or "Hill" in the name.
    LVL 3

    Expert Comment

    yeah, you can choose whatever you want. AND will give limited results than OR.
    LVL 6

    Expert Comment

    $search= preg_replace('/^the/i','',$search);

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now