<?php
// first check for the submited search data
if (!isset ($_GET['search'])) //if search data not submited, you get the form to enter search string
{
echo '
<form action="" method="get" name="form1" target="_blank">
<input name="search" type="text" id="search" size="30">
<input type="submit" name="Submit" value="cauta">
</form>';
die (); //stop the script
}
//if search data submited start preparing the search string
$search = $_GET['search'];
$search = preg_replace("/[\s+,]+/", " ", $search); //replace any commas and/or spaces with single spaces
$search = preg_replace("/\"\s*\"/", "", $search); // remove any empty string between quotes
/* create the $rez array from the quoted part(s) of $search string that must be treated as block(s) of text;
$rez is an array who's values are also arrays; the first one of theese arrays is the one with our quoted strings as elements;
so... we index a new array, $str, with $i=0 as start index;
in this array we'll parse all the blocks of text we want to search the database (first the quoted text as blocks, than the unquoted text as words) */
$quote_str = preg_match_all ("/\"(?:\s*\w*\s*)+\"/", $search, $rez);
//create the $str array and parse the quoted blocks of text as values of this array
$i=0;
foreach ($rez as $key => $value)
{
foreach ($value as $key => $value)
{
$str[$i] = preg_replace("/\"+/", "", $value); //remove quotes so we can keep only the blocks of text
//echo "str[$i] = ".$str[$i]."<br>"; //this line is for testing purposes only
$i++;
}
}
// now we remove the quoted blocks of text from the search string
$search = preg_replace("/\"(\s*\w*\s*)+\"/", "", $search);
/*sellect each single word (with more than 4 characters;
I thaught that any smaller isolated word is irelevant for the search, but you may change this value if you wish)*/
$text = preg_match_all ("/(?:\w{4,})/", $search, $rez);
//continue parsing values in $str array, this time from the isolated words of the search
foreach ($rez as $key => $value)
{
foreach ($value as $key => $value)
{
$str[$i]= $value;
//echo "str[$i] = ".$str[$i]."<br>"; //this line is for testing purposes only
$i++;
}
}
//initialize the search string for database query
$search_str = '';
// select from the database the columns you want to search in and assign their names as the $key variables in $search_str bellow
$query = "SELECT column_1, column_2, ..., column_x FROM table";
$result = mysql_query($query);
$columns = mysql_fetch_assoc ($result);
foreach ($columns as $key => $value) // for each column we want to search
{
/* for each value (block of text or word) from $str we add a a pair like
this string "OR column_name LIKE '%$value%' " to $search_str*/
foreach ($str as $indice => $valoare)
{
$search_str .= "OR ".$key." LIKE '%$valoare%' ";
}
}
//now we have a database search string ($search_str) that starts with an "OR" that we'll replace with a parathesis
$search_str = preg_replace("/^OR/", "(", $search_str).")";
$search_str .= "AND column_m='1' AND column_k>0"; // add any other conditions with OR or AND
//echo $search_str; //this line is for testing purposes only
// now query the dB with the string you've created and have your results
$query = "SELECT DISTINCT column_1, column_2, ..., column_x FROM table
WHERE $search_str ORDER BY column_z ASC";
$result = mysql_query($query);
$nr=mysql_num_rows($result);
if ($nr==0) {die ('Sorry! No reccord found!');}
for ($k=0; $k<$nr; $k++)
{
$rand= mysql_fetch_assoc ($result);
foreach ($rand as $key => $value)
{
echo "$key = $value<br>";
}
echo "<br><br>";
}
?>
I hope that helps; I spent 2 nights to make it work and... it works; you probably can simplify the code, but I think it's a good start; if you have an easier solution to this problem, i'd be glad to know 🙂
www.arhimedia.ro