Re: preg_match_all - how does it work?
| From: | Marco Tabini | Date: | Sun, 17 Nov 2002 16:38:22 +0000 |
| Subject: | Re: preg_match_all - how does it work? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124799@lists.php.net to get a copy of this message | ||
Hi Randall,
If you look closely at the definition of preg_match_all(), you'll see
that it generates an array of arrays--that's because, for each match, it
generates an array that corresponds to the result of a single call to
preg_match, then puts all the resulting array into another array.
If you use PREG_SET_ORDER, your $match array contains an array for each
of the matches found. Each array then contains:
* The entire matched string in element 0
* Each of the substring (defined in brackets in your pattern) starting
from array 1.
Hope this helps!
Marco
--
------------
php|architect - The magazine for PHP Professionals
The first monthly worldwide magazine dedicated to PHP programmers
Check us out on the web at http://www.phparch.com
On Sun, 2002-11-17 at 11:13, Randall Perry wrote:
> I have some experience in using regular expressions in Perl, and have been
> able to use preg_match in php, but am having a problem with preg_match_all.
>
> I'm trying to extract data from an HTML table. Here's my preg_match call
> that correctly grabs the 1st row of data in the table. It grabs 4 columns of
> data:
>
> preg_match("|<tr
>
> class=\"CART_TD_REG\".+?<td.+?>(.*?)<.+?<td.+?>(.*?)<.+?<td.+?>(.*?)<.*?<inp
> ut.*?>[ ]*(.*?)</td>.*?</tr>|", $res, $match);
>
> If I use preg_match_all on the same table:
> preg_match_all("|<tr
>
> class=\"CART_TD_REG\".+?<td.+?>(.*?)<.+?<td.+?>(.*?)<.+?<td.+?>(.*?)<.*?<inp
> ut.*?>[ ]*(.*?)</td>.*?</tr>|", $res, $match, PREG_PATTERN_ORDER);
>
> ...and try to print output, I get:
>
> $match[0] = Array
> $match[0][0] = Array[0]
> $match[1][0] = Array[0]
> $match[0][1] = Array[0]
> $match[1][1] = Array[0]
>
>
> Same result with PREG_SETORDER.
>
> What am I doing wrong?
>
> --
> Randall Perry
> sysTame
>
> Xserve Web Hosting/Co-location
> Website Development/Promotion
> Mac Consulting/Sales
>
> http://www.systame.com/
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>