PDA

View Full Version : Help with search engine


Nightfire
01-04-2003, 09:59 AM
I'm having some real problems thinking of how to do this. I'm creating a search engine and I'm wanting to delete most used words out of the string ppl are searching for, ie:

$textentered = "a free house and car";
and delete the words a and and

So I'm wanting to split it at the spaces then check what's in each part splitted?

At the mo I'm using

$text = " hello";

$text = strtolower($text);

//$text = explode(" ",$text);

$text = ereg_replace(" is", "", $text);
$text = ereg_replace(" and", "", $text);
$text = ereg_replace(" or", "", $text);

$dbhost = "localhost";
$dbuname = "root";
$dbpword = "";
$dbname = "searchengine";

@mysql_connect($dbhost,$dbuname,$dbpword);
@mysql_select_db($dbname);
$sql = "SELECT * FROM `sites` WHERE description like '%$text%'";
$result = mysql_query($sql);

$affected = mysql_num_rows($result);

if($affected > 0){

while($row = mysql_fetch_array($result)){
$row[2] = ereg_replace("$text",'<font color="#ff0000">'.$text.'</font>',$row[2]);

echo $row[1].'<br>'.$row[2];
}

}else{
echo 'No results found';
}

When I use explode, I get "no results found" I think I need to use an array, but I don't understand them :confused:

Me so lost :(

Kiwi
01-04-2003, 10:55 AM
Explode removes all the spaces from your string and returns an array, so your regex don't have any matches and then your query string will have an object in it, which it won't like. To be sure of catching the right things, you might want to use something like:
$text=strtolower($text);
$text=preg_replace("/\s+a|and|or\s+/", " "); // finds all of the common words in one hit
$a_text=explode($text);
//connect to database
foreach($a_text as $key) {
// do your database query
}Just another note -- you only need two variables from your query, so you could specify these two fields in your sql instead of pulling all the fields.

Nightfire
01-04-2003, 12:37 PM
Thanks, I'll give it a try :)

What does this line actually do?


foreach($a_text as $key) {


I've never seen that before :)

Kiwi
01-04-2003, 06:40 PM
http://www.php.net/manual/en/control-structures.foreach.php

Much easier than me trying to explain it. It basically parses the array for you.