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