Re: Search/Regular Expression problem
| From: | Chris Hayes | Date: | Thu, 13 Mar 2003 21:02:28 +0000 |
| Subject: | Re: Search/Regular Expression problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139595@lists.php.net to get a copy of this message | ||
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.