...

View Full Version : Comparing Arrays



StupidRalph
01-29-2007, 10:38 AM
I have a comma delimited column for keywords in a DB. I was wondering the best way to compare these keywords with user submitted search terms.

I was thinking about putting the user entered text in an array and then comparing that array for each keyword array in the DB.

Other than me having a hard time with accomplishing this...I don't think this is the most efficient way of going about this.
This is what I have for parsing the user input search terms:



$input = basicClean($_GET['input']); //some basic string cleaning
$input = preg_replace('/[^a-z0-9&\s]/i', '', $input); //only allows alphanumeric characters, ampersand (&), and spaces.
$input = trim($input); // trims beginning and trailing space from text.
$input = strtolower($input); //makes the text lowercase
$input = explode(" ",$input); //creates an array delimited by spaces
$input = array_filter($input); //removes emptry strings, null values, and values that equate to false
$input = array_merge(array_flip(array_flip($input))); //removes duplicate entries

I just didn't know what to do beyond this to be able to compare to the keyword array for each row in the DB and rank each.

I'm open to any suggestions whether its a 3rd party class script or totally different approach.

I wanted to add that the keywords are not for actual pages but for business locations. The pages are created dynamically. So I would need to make a reference for each location ID.

StupidRalph
01-29-2007, 01:46 PM
Okay so with a little help of a foreach statement and the in_array function I am able to test check the user entered data against whats in the database and count the number of occurences.



$_GET['input'] = "Income Tax Rapid Refund"; // Example of what a user may enter as search text.
$input = basicClean($_GET['input']); //some basic string cleaning
$input = preg_replace('/[^a-z0-9&\s]/i', '', $input); //only allows alphanumeric characters, ampersand (&), and spaces.
$input = trim($input); // trims beginning and trailing space from text.
$input = strtolower($input); //makes the text lowercase
$input = explode(" ",$input); //creates an array delimited by spaces
$input = array_filter($input); //removes emptry strings, null values, and values that equate to false
$input = array_merge(array_flip(array_flip($input))); //removes duplicate entries and reindexes the keys

$haystack = explode(",", "money,managers,accounting,rapid,refund"); //mock example of keywords array from database.

$occurences = 0; // init variable for how many times the keywords
foreach ($input as $needle) {

if (in_array($needle,$haystack)) { //check to see if the input keyword is in the DB keyword array.
$occurences++; //its here so add one for occurence
}

}
echo "there are " . $occurences . " occurences of matching keywords";


So I'm guessing my next step is to put the ID for each row in an array along w/ the number of occurences and then do a sort on the occurences.

StupidRalph
01-30-2007, 02:30 PM
Seek and thou shall find...

http://dev.mysql.com/doc/refman/4.1/en/fulltext-search.html
This is sooooooooo much easier lol.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum