Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Removing "The" from searches

Posted on 2005-04-27
5
Medium Priority
?
190 Views
Last Modified: 2008-03-06
Hi!

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:
<?php
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
0
Comment
Question by:Aknot
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
German_Rumm earned 90 total points
ID: 13874387
Hi Aknot,

There are several solutions for your problem.
Probably the most effective will be using FULLTEXT search (http://dev.mysql.com/doc/mysql/en/fulltext-search.html)
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);

HTH!
0
 
LVL 3

Assisted Solution

by:designbai
designbai earned 60 total points
ID: 13875139
if you are going for a fulltext search, before your move, read the following.

http://dev.mysql.com/doc/mysql/en/fulltext-restrictions.html

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
0
 
LVL 6

Expert Comment

by:German_Rumm
ID: 13875238
designbai,

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

Expert Comment

by:designbai
ID: 13875436
yeah, you can choose whatever you want. AND will give limited results than OR.
0
 
LVL 6

Expert Comment

by:mmarth
ID: 13887388
$search= preg_replace('/^the/i','',$search);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

578 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