title: Preg_match text file and display multiple results in a single code
body: <p>I have a text file ("file.txt"):</p>
  
  <pre><code>5 (blah-blah) 001  
  2 (blah) 006  
  </code></pre>
  
  <p>With a PHP code to find the first word, expression in parentheses, and last 3- or 4-digit numbers by searching for a pattern in line[number]:  </p>
  
  <pre><code>&lt;?php  
  // file  
  $file = file("file.txt");  
  
  /* first line */
  // match first word (number)  
  preg_match("/^(\d+)/",$file[0],$first_word);  
  
  // match expression within parentheses  
  preg_match("/(?&lt;=\().*(?=\))/s",$file[0],$within_par);  
  
  // match last 3- &amp; 4-digit numbers  
  preg_match("/(\d{3,4})?(?!.*(\d{3,4}))/",$file[0],$last_word); 
  
  /* repeats (second line) */  
  preg_match("/^(\d+)/",$file[1],$first_word2);  
  preg_match("/(?&lt;=\().*(?=\))/s",$file[1],$within_par2);  
  preg_match("/(\d{3,4})?(?!.*(\d{3,4}))/",$file[1],$last_word2); 
  &lt;?php
  </code></pre>
  
  <p>And an HTML code to display the matches line by line:</p>
  
  <pre><code>&lt;div&gt;
      &lt;p&gt;&lt;?php echo $first_word[0] ?&gt;&lt;/p&gt;
      &lt;p&gt;&lt;?php echo $within_par[0] ?&gt;&lt;/p&gt;
      &lt;p&gt;&lt;?php echo $last_word[0] ?&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;p&gt;&lt;?php echo $first_word2[0] ?&gt;&lt;/p&gt;
      &lt;p&gt;&lt;?php echo $within_par2[0] ?&gt;&lt;/p&gt;
      &lt;p&gt;&lt;?php echo $last_word2[0] ?&gt;&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But I would like to be able to display all the matches without having to list each one individually, both in my PHP code and HTML code. I would like to use preg_match_all to search in the text file, then foreach all matches, and echo/return each one, one div (with three patterns) at a time. (I have tried several different ways but I get an Array as a result.) What code can accomplish this?</p>