title: PHP Search Array With Multiple Keywords And Sorted Result
body: <p>I have a plan to make search from txt file that I prepare, the txt file content similar like this below</p>
  
  <p>a.txt</p>
  
  <pre><code>Amy Jefferson
  Nathalie Johnson
  Emma West
  Donna Jefferson
  Tanya Nathalie
  George West
  Emma Watson
  Emma Jefferson
  </code></pre>
  
  <p>If the code was like this </p>
  
  <p>a.php</p>
  
  <pre><code>$filename = "a.txt";
  $example = file($filename, FILE_IGNORE_NEW_LINES);
  $searchword = 'Emma Jefferson';
  $matches = array();
  foreach($example as $k=&gt;$v) {
      if(preg_match("/\b$searchword\b/i", $v)) {
          $matches[$k] = $v;
          echo $matches[$k]."&lt;br&gt;";
      }
  }
  </code></pre>
  
  <p>The result will only "Emma Jefferson" </p>
  
  <p>Then if i use this code</p>
  
  <p>b.php</p>
  
  <pre><code>$filename = "a.txt";
  $example = file($filename, FILE_IGNORE_NEW_LINES);
  $searchword = 'Emma Jefferson';
  $matches = array();
  foreach($example as $k=&gt;$v) {
      $searchword2 = str_ireplace(" ", "|", $searchword);
      if(preg_match("/\b$searchword2\b/i", $v)) {
          $matches[$k] = $v;
          echo $matches[$k]."&lt;br&gt;";
      }
  }
  </code></pre>
  
  <p>The result will be like this</p>
  
  <pre><code>Amy Jefferson
  Emma West
  Donna Jefferson
  Emma Watson
  Emma Jefferson
  </code></pre>
  
  <p>Unique result, but "Emma Jefferson" in the last result</p>
  
  <p>So the question is how I can search Emma Jefferson, the result sort was like this</p>
  
  <pre><code>Emma Jefferson
  Emma Watson
  Emma West
  Amy Jefferson
  Donna Jefferson
  </code></pre>
  
  <p>So basically it search for "Emma Jefferson" entire word first, then "Emma" and the last one is "Jefferson"</p>
  
  <p><strong>UPDATE</strong>
  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!!</p>
  
  <p>Pattygeek</p>