You want to find the nth word match instead of the first one.
Use preg_match_all("/regular expression/", "string", $matches) to pull all the matches into an array; then pick out the specific matches in which you're interested. The first element of the array holds an array of matches of the complete pattern. Subsequent matches of the array hold arrays of text matched by each parenthesized subpattern.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php if (isset($_POST['submit'])) { process_form(); } else { display_form();// display form for the first time } function display_form() { echo <<<HTML <form action = "$_SERVER[SCRIPT_NAME]" method="post"> Regular Expression Pattern (include the delimiter):<br /> <input type="text" name="pattern" value="/\d\. ([^\d]+)/" /><br /> Text:<br /> <textarea name="text" rows="5" cols="50"> 1. Go to work 2. Do exercise 3. Play tennis 4. Go shopping </textarea><br /> <input type="submit" name="submit" value="Check" /> </form> <table border> <tr bgcolor="lightcyan"> <th align="left">Regular Expression</th> <th align="left">Text</th> </tr> <tr> <td>/love/</td> <td>My lovely gloves are worse for wear, Love. </td> </tr> </table> HTML; } function process_form() { // stripslashes and trim the strings $pattern = stripslashes(trim($_POST['pattern'])); $text = trim($_POST['text']); echo "The result of checking <br /><font color=red>$pattern</font><br /> against<br /><font color=red>$text</font><br /> is:<br />"; //$matches[0] holds the parts of $text that matches /\d\. ([^\d]+)/ preg_match_all($pattern, $text, $matches); echo "<p>This is the first element of the matches array $matches[0]:</p>"; print_r($matches[0]); //matches[1] is an array of each substring captured by ([^\d]+) echo "<br /><br />The second on the to do list is: "; echo $matches[1][1]; echo "<p>The entire to do list is:</p>"; foreach ($matches[1] as $key =>$match) { echo $key, " => ", $match, "<br />"; } echo "<br /><a href=\"$_SERVER[SCRIPT_NAME]\">Check again?</a>"; } ?> </p> </body> </html> |
Instead of returning an array into full matches and then submatches,
preg_match_all() can return an array divided by matches, with each submatch inside. Use PREG_SET_ORDER as the fourth argument.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php if (isset($_POST['submit'])) { process_form(); } else { display_form();// display form for the first time } function display_form() { echo <<<HTML <form action = "$_SERVER[SCRIPT_NAME]" method="post"> Regular Expression Pattern (include the delimiter):<br /> <input type="text" name="pattern" value="/([a-zA-Z]+)=(.*)/" /><br /> Text:<br /> <textarea name="text" rows="5" cols="50"> first=Go to work second=Do exercise third=Play tennis fourth=Go shopping </textarea><br /> <input type="submit" name="submit" value="Check" /> </form> <table border> <tr bgcolor="lightcyan"> <th align="left">Regular Expression</th> <th align="left">Text</th> </tr> <tr> <td>/([a-zA-Z0-9]+).(.*)/</td> <td><pre> 1. Go to work 2. Do exercise 3. Play tennis 4. Go shopping </pre> </td> </tr> </table> HTML; } function process_form() { // stripslashes and trim the strings $pattern = stripslashes(trim($_POST['pattern'])); $text = trim($_POST['text']); echo "The result of checking <br /><font color=red>$pattern</font><br /> against<br /><font color=red>$text</font><br /> is:<br />"; //$matches[0] holds the parts of $text that matches /\d\. ([^\d]+)/ preg_match_all($pattern, $text, $matches, PREG_SET_ORDER); //$match[0] is the entire matched string // $match[1] the bit before the = //$match[2] the bit after = echo "<p>The entire to do list is:</p>"; foreach ($matches as $match) { echo $match[1], " => ", $match[2], "<br />"; } echo "<br /><a href=\"$_SERVER[SCRIPT_NAME]\">Check again?</a>"; } ?> </p> </body> </html> |