|
|
|
the Problem:
I am making a search engine and I would have the user to type a sentence to search for. Let's say
it is stored in $search. My search engine 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
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);
if (eregi(".*words[0].*words[1].*words[2].*", 'possible result')) {
.....
}
?>
Even if I know how many words there are, every time the number of words in $search can be different.
another note is that my data is taken from a database.
The Solution:
If you have used explode() 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. |
|
| SiteSearch 1.1:
This app lets end users search your site for keywords. You specify which directories should be included in the search. Categories : Search, Search Engines, PHP | | | Extract keywords from a string having words in " " count as one string. Categories : PHP, Strings, Regexps, Search | | | Check if a file exists on a remote FTP server with PHP Categories : PHP, FTP, Regexps | | | Searches through a local INN server's discussions Categories : Search, Complete Programs, PHP | | | Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS) | | | PageRank Display Categories : Search Engines, HTML and PHP, PHP | | | ereg -- Regular expression match Categories : PHP, PHP Functions, Regexps | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | Boolean Keyword Interpreter Categories : PHP, Algorithms, Search Engines | | | Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP Categories : PHP, HTTP, Regexps | | | BBCode Formatting String Categories : PHP, HTML, Regexps, Arrays | | | UDMSearch - a free search engine, indexing system. Categories : Search Engines, Linux, PHP, MySQL, ODBC | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | This script is a contact form between users of a
website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps | |
| | | | Jasp Jones wrote : 908
not sure of the php internals but would it be better to get the count before the for loop to save re-evaluating the count on each loop? also a nice way to get around chopping the first `AND` off is to put `WHERE 1 = 1` || $ANDS
cheers
Jasp
| | | | Roberto Schiabel wrote :917
what if your search must apply more than one field.
do you use a nested-cicle to build a sql statement to mach all the words in every field ?
thanks
(sorry for my english)
| |
|
|
|