read file matching lines using array using php
<p>I want to be able to read through a plain text file and match a number of lines without the need to iterate over the text file multiple times. I am passing in an array with a list of strings I would like to match, which ideally, I would like to put into an array.</p>

<p>I can achieve the desired result using the code below, but it necessitates the reading of the text file multiple times.</p>

<pre><code>function readFile($line){
  $contents = file("test.txt");

  if(preg_match("/$line*/i", $val)){
    return($val);
  }
}
</code></pre>

<p>Ideally, I would like to do the following:</p>

<pre><code>// pass an array to the funciton which will parse the file once and match on the elements defined.
$v = readFile(array("test_1", "test_2", "test_2", "test_3"));

// return an array with the matched elements from the search.
print_r($v);
</code></pre>

<p>Any help would be much appreciated.</p>

<p>Thanks all!</p>
