I'm currently creating a search engine for a database and realized that numbers or short words would mess up the search.
The engine works so that it splits the sentence into words, and each word are put in to an array called $searches
.
An example of a search that creates a mess would look something like:
database info 3
Each word will be looked for everywhere and shown if it matches either search 1 and 2, 1 and 3, or 2 and 3. The problem is, that by searching "3", it creates a mess. Because there are so many things containing just a single character.
So I wonder how I can check the length of all content in an array, and loop this correctly.
This is the code I have so far (Not fully):
$search = $_GET['q']; //Search string
$count = 0; //Counter
$searches = explode(' ', $search); //Creates the array
for ($i = 0; $i < count($searches); $i++) { //Loop for each element in array
if (strlen($searches[$i]) <= 2) { //if it finds an element less than 2 characters
$keyword = $searches[$i]; //remember key word
foreach (glob('database/*/*/*.txt') as $path) { //Look through database
$title = basename($path, ".txt").PHP_EOL; //Get file instead of path
for ($q=0; $q < count($searches); $q++) { //Another loop in case keyword comes last (which it does)
if (($searches[$q] != $keyword) && (strripos($title,$keyword) != false) && (strripos($title,$searches[$q]) != false)) { //check if if keyword is not equal to itself while searching and tries to find a file with a combination of keyword and $searches[$q].
echo $title . '<br>'; //Gives output
$count += 1;
}
}
}
}
}
if($count == 0) {
echo 'NOTHING'; //Nothing
}
It seams like it won't output any files including both words. Basically not showing any files. The double loop just makes this complicated for checking the array length. Any clue how to get this to work?