Re: RE: [PHP-DB] Searching MySQL using LIKE
Try: REGEXP 'keyword?'; //Matches Zero or one instances of 'keyword' REGEXP 'keyword+'; //Matches One or more instances of 'keyword' REGEXP 'keyword*'; //Matches Zero or more instances of 'keyword' Cheers. Russ '; On Fri, 21 Sep 2001 04:14:47 -0700 Ralph Guzman [EMAIL PROTECTED] wrote: Postfix Users, e need to try and help out in any way I can. So here goes: Jord, Try using REGEXP rather than LIKE. So try something like this: SELECT * FROM table WHERE keywords REGEXP '%keyword%'; If this does not work I may have the wrong string pattern, if so, do a search on REGEXP on the mySQL manual: http://www.mysql.com/doc/. I'm sure they have examples on using REGEXP. Good Luck. -Original Message- From: Jordan Elver [mailto:[EMAIL PROTECTED]] Sent: Friday, September 21, 2001 9:03 AM To: MySQL Mailing List; PHP DB Mailing List Subject: [PHP-DB] Searching MySQL using LIKE Hi, I'm cross posting this because it's applicable to both list ;-) I have a table which contains a field called Keywords. The field typically contains something similar to this rubbish bins trash I want to search this field and I'm having trouble. If someone enters I need bins then the the result will not be found. I started off by using SELECT * FROM table WHERE keywords LIKE '%keyword%', but that doesn't work if you type in more than just bins. So then I tried splitting the words entered on a space and searching for each of them like this SELECT * FROM table WHERE keywords LIKE '%keyword1%' OR keywords LIKE '%keyword1%'. But that returns loads of results because a lot of words contain is or it etc. Can anyone help me with this one, I'm not sure what to try next? Cheer, Jord -- Jordan Elver Web Developer The InternetOne UK Ltd -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] #---# Believe nothing - consider everything Russ Michell Anglia Polytechnic University Webteam Room 1C 'The Eastings' East Road, Cambridge e: [EMAIL PROTECTED] w: www.apu.ac.uk/webteam t: +44 (0)1223 363271 x 2331 www.theruss.com #---# -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Searching MySQL using LIKE
Jason Wong [EMAIL PROTECTED] wrote in message news:000301c1428d$f11b3a80$[EMAIL PROTECTED]... 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%'; ? -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]