Re: Searching MySQL using LIKE

From: Date: Fri, 21 Sep 2001 15:13:35 +0000
Subject: Re: Searching MySQL using LIKE
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-12639@lists.php.net to get a copy of this message
"Jason Wong" <phplist@gremlins.com.hk> wrote in message news:000301c1428d$f11b3a80$0100000a@gremlins.com.hk... > The obvious first thing to do is to screen your keywords. Do not search on > common words such as: > > and, if, the, in etc. An easy way to do this is to set up an array of words to exclude. Then you can parse your input string and use array_diff() to return only the non-excluded words, ie <?php // Assume we get $searchphrase from a form // Strip all non-alpha text from string; // anyone know a more convenient f'n for this? // NOTE: this list is quite incomplete. // NOTE: can also use this step to convert // accented characters to their plain counterparts. $conv = array( "0" => "", "1" => "", "2" => "", "3" => "", "4" => "", "5" => "", "6" => "", "7" => "", "8" => "", "9" => "", "!" => "", "&" => "", "\n" => "", "\t" => "", "." => "", "<" => "", ">" => "", "\\" => "", ); $searchphrase = strtr(strtolower($searchphrase), $conv) // Parse input string into an array of words $use = explode(" ", $searchphrase); // Add any other too-common words that occur to you. $excl = array( 'i', 'you', 'he', 'she', 'it', 'am', 'is', 'be', 'are', 'to', 'from', 'if', 'the', 'this', 'that', 'in', 'of', 'at', 'a', 'an', 'and', 'or', 'not' ); // Filter out too-common words $use = array_diff($use, $excl); if (count($use) == 0) { echo "No valid search terms"; exit(); } // The FALSE is a dummy term so I don't // have to muck about deciding whether an // OR is needed for each search term. $query = 'SELECT * FROM table WHERE FALSE'; foreach($use as $term) $query .= " OR keywords LIKE '%$term%'"; ?>

« previous php.db (#12639) next »