I have a text file ("file.txt"):
5 (blah-blah) 001
2 (blah) 006
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]:
<?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("/(?<=\().*(?=\))/s",$file[0],$within_par);
// match last 3- & 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("/(?<=\().*(?=\))/s",$file[1],$within_par2);
preg_match("/(\d{3,4})?(?!.*(\d{3,4}))/",$file[1],$last_word2);
<?php
And an HTML code to display the matches line by line:
<div>
<p><?php echo $first_word[0] ?></p>
<p><?php echo $within_par[0] ?></p>
<p><?php echo $last_word[0] ?></p>
</div>
<div>
<p><?php echo $first_word2[0] ?></p>
<p><?php echo $within_par2[0] ?></p>
<p><?php echo $last_word2[0] ?></p>
</div>
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?