I have a plan to do a search from the txt file that I am preparing, the contents of the txt file are similar to 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 that,
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 be 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
The only result, but Emma Jefferson in the last result
So the question is, how can I look for Emma Jefferson, the result was like this.
Emma Jefferson Emma Watson Emma West Amy Jefferson Donna Jefferson
So, basically he searches for the word "Emma Jefferson" first, and then "Emma", and the last - "Jefferson"
UPDATE I vote for "Not a panic code for this problem," but I want to thank you for all the participants here. Do not panic, RomanPerekhrest, Sui Dream, Jere, i-man, all of you are the best!
Pattygeek