PHP Search Array With Multiple Keywords And Sorted Result

Viewed 610

I have a plan to make search from txt file that I prepare, the txt file content similar like this below

a.txt

Amy Jefferson
Nathalie Johnson
Emma West
Donna Jefferson
Tanya Nathalie
George West
Emma Watson
Emma Jefferson

If the code was like this

a.php

$filename = "a.txt";
$example = file($filename, FILE_IGNORE_NEW_LINES);
$searchword = 'Emma Jefferson';
$matches = array();
foreach($example as $k=>$v) {
    if(preg_match("/\b$searchword\b/i", $v)) {
        $matches[$k] = $v;
        echo $matches[$k]."<br>";
    }
}

The result will only "Emma Jefferson"

Then if i use this code

b.php

$filename = "a.txt";
$example = file($filename, FILE_IGNORE_NEW_LINES);
$searchword = 'Emma Jefferson';
$matches = array();
foreach($example as $k=>$v) {
    $searchword2 = str_ireplace(" ", "|", $searchword);
    if(preg_match("/\b$searchword2\b/i", $v)) {
        $matches[$k] = $v;
        echo $matches[$k]."<br>";
    }
}

The result will be like this

Amy Jefferson
Emma West
Donna Jefferson
Emma Watson
Emma Jefferson

Unique result, but "Emma Jefferson" in the last result

So the question is how I can search Emma Jefferson, the result sort was like this

Emma Jefferson
Emma Watson
Emma West
Amy Jefferson
Donna Jefferson

So basically it search for "Emma Jefferson" entire word first, then "Emma" and the last one is "Jefferson"

UPDATE I vote for Don't Panic code for this problem, but i wanna say thank you for all contributor here Don't Panic, RomanPerekhrest, Sui Dream, Jere, i-man, all of you are the best!!

Pattygeek

5 Answers

I don't know of a way to take the position of the matches into account with a regex solution, but if you convert the search string and the terms to arrays of words, it can be done.

With this approach, we iterate the text items and build an array of position matches for each word in the search term, then sort the result by number of matches, then position of matches.

$search_words = explode(' ', strtolower($searchword));

foreach ($example as $item) {
    $item_words = explode(' ', strtolower($item));

    // look for each word in the search term
    foreach ($search_words as $i => $word) {
        if (in_array($word, $item_words)) {

            // add the index of the word in the search term to the result
            // this way, words appearing earlier in the search term get higher priority
            $result[$item][] = $i;
        }
    }
}

// this will sort alphabetically if the uasort callback returns 0 (equal)
ksort($result);

// sort by number of matches, then position of matches    
uasort($result, function($a, $b) {
    return count($b) - count($a) ?: $a <=> $b;
});

// convert keys to values    
$result = array_keys($result);

You currently echo results immediately, so they ordered as they are in text.

You can search full string and partial matches, and then concatenate results.

foreach($example as $k=>$v) {
    if(preg_match("/\b$searchword\b/i", $v)) {
        $fullMatches[] = $v;
    }
    if(preg_match("/\b$searchword2\b/i", $v)) {
        $matches[] = $v;
    }
}
$matches = array_unique(array_merge($fullMatches, $matches));
foreach($matches as $k => $v)
    echo $v . "<br>";

Update:

Multiple words variant:

$words = ['Emma', 'Jefferson'];
$matches = array();
foreach($example as $k => $v) {
    $fullStr = implode(' ', $words);
    if(preg_match("/\b$fullStr\b/i", $v))
        $matches[0][] = $v;
    $str = "";
    $i = 1;
    foreach($words as $word) {
        if ($str === "")
            $str = $word;
        else
            $str .= '|' . $word;
        if(preg_match("/\b$str\b/i", $v))
            $matches[$i][] = $v;
        $i++;
    }
}
$result = array();
foreach($matches as $firstKey => $arr) {
    foreach($arr as $secondKey => $v) {
        $result[] = $v;
    }
}
$result = array_unique($result);
foreach($result as $k => $v)
    echo $v . "<br>";

Complex solution:

$lines = file('a.txt', FILE_IGNORE_NEW_LINES);
$name = 'Emma';
$surname = 'Jefferson';
$emmas = $jeffersons = [];

foreach ($lines as $l) {
    if (strpos($l, $name) === 0) {
        $emmas[] = $l;
    } elseif ( strrpos($l, $surname) === (strlen($l) - strlen($surname)) ) {
        $jeffersons[] = $l;
    }
}

usort($emmas, function($a,$b){
    return strcmp(explode(' ', $a)[1], explode(' ', $b)[1]);
});
usort($jeffersons, function($a,$b){
    return strcmp($a, $b);
});

$result = array_merge($emmas, $jeffersons);
print_r($result);

The output:

Array
(
    [0] => Emma Jefferson
    [1] => Emma Watson
    [2] => Emma West
    [3] => Amy Jefferson
    [4] => Donna Jefferson
)

You would have to write a new loop or start sorting your Array afterwords, because the foreach-loop takes one element name at the time, tests if it matches your search word and if it does, the name goes at the end of your new Array $matches[]. So the

    if(preg_match("/\b$searchword2\b/i", $v)) {
    $matches[$k] = $v;
    echo $matches[$k]."<br>";
}

part does not know anything about the names that are or aren't already inside of $matches[].

So my suggestion would be:

$filename = "a.txt";
$example = file($filename, FILE_IGNORE_NEW_LINES);
$searchword = 'Emma Jefferson';
$matches = array();



$searchword2 = array($searchword, explode(" ", $searchword)[0], explode(" ", $searchword)[1]);
$isThisNameAlreadyInTheList;

foreach($searchword2 as $actualSearchword) {

    foreach($example as $k=>$v) {

        $isThisNameAlreadyInTheList = false;
        foreach($matches as $match) {   
            if(preg_match("/\b$match\b/i", $v)) {
                $isThisNameAlreadyInTheList = true;
            }
        }

        if (!$isThisNameAlreadyInTheList) {
            if(preg_match("/\b$actualSearchword\b/i", $v)) {
                $matches[$k] = $v;
                echo $matches[$k]."<br>";
            }
        }
    }

}

I would use a preg_match_all solution like so:

$searchName = "Emma Jefferson";
$searchTerms = explode(' ', $searchName);

$pattern = "/(\b$searchTerms[0]\b \b$searchTerms[1]\b)|(\b$searchTerms[0]\b \w+)|(\w* \b$searchTerms[1]\b)/i";

$output = [];
preg_match_all($pattern, implode(' | ', $example), $out);

foreach($out as $k => $o){
    if($k == 0){
        continue;
    }

    foreach($o as $item){
        if(!empty($item)){
            $output[] = $item;
        }
    }
}

print_r($output);

You could also bring the file in as a string and avoid the implode portion.

Related