WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : How to build a search query for any N number of words in a search string
Categories : PHP, Regexps, Search Engines, Search Update Picture
Kelvin Poon
Date : Mar 14th 2003
Grade : 4 of 5 (graded 1 times)
Viewed : 8208
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Kelvin Poon
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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)