Yeah my data are from a database. How do I use query to search something like that? -----Original Message----- From: Chris Hayes [mailto:[EMAIL PROTECTED] Sent: Thursday, March 13, 2003 4:02 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] Search/Regular Expression problem
At 21:48 13-3-2003, you wrote: >My search enginue will bring up a result IF and only if ALL of the words in >$search exist in the result. For example >if > >$search = Vax Useful Commands > >Then the result is only true if (eregi(".*Vax.*Useful.*Commands.*", >'possible result')) is true Are your data in a database? In that case it is much faster to use a query to check whether the words are in some record. Regular expressions can take quite some time! >Now, I don't know how many words there are in $search initially. How do I >do a search like that? I mean if I know $search is 3 words then I can do > >$words = preg_split("/ /", $search); I like the 'explode()' function very much , which splits a string on a separator (can be ' ') and puts the results in a long array. >if (eregi(".*words[0].*words[1].*words[2].*", 'possible result')) { > ..... >} > > >Even if I know how many words there are, everytime the number of words in >$search can be different. So if you have used explode() then you can do a loop that goes through the array, for instance $searchwords=explode(' ', $search); for ($x=0;$x<count($searchwords);$x++) {//build the search command OR build the query // i recommend to make it a query, so: $WHERE.= ' AND textfield=LIKE '"%'.$searchwords[$x].'%"'; } //now cut off the first ' AND ' $WHERE=substr($WHERE,4,strlen($WHERE); now finish the query $query="SELECT title,text,ID FROM tablename ".$WHERE; then proceed by doing the query and showing the results. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php