RE: [PHP] preg_match fails to fill $matches
| From: | John W. Holmes | Date: | Thu, 06 Mar 2003 04:14:12 +0000 |
| Subject: | RE: [PHP] preg_match fails to fill $matches | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-138409@lists.php.net to get a copy of this message | ||
> >> The full code in the loop is.
> >> Later on in the code after the loop I do
> >>
> >> echo "printing array matches <br>";
> >> print_r ($matches);echo "<br>";
> >
> >$matches is going to be over-written after every time through the
loop!
> It
> >doesn't add it all up.
> >
> >Add in a
> >
> >$var[] = $matches;
> >
> >or something inside of your loop to keep track of them all, then
> >print_r($var) to see what it contains.
>
> Thanks that fixed it I ended up with. with the $ts string | and | the
|
>
> $hit[] = preg_match ($ts, $line_in,$var);
> $matches[] = $var;
>
> but it still it needed $var = array(); before it would work. The
> results are interesting and shows I don't understand arrays :(
>
> print_r ($matches);
>
> printing array matches (reformatted)
> Array (
> [0] => Array ( )
> [1] => Array ( )
> [2] => Array ( )
> [3] => Array ( )
> [4] => Array ( )
> [5] => Array ( )
> [6] => Array ( [0] => and [1] => and )
> [7] => Array ( [0] => the [1] => the )
> [8] => Array ( [0] => and [1] => and )
> [9] => Array ( )
> [10] => Array ( )
> [11] => Array ( )
> [12] => Array ( [0] => and [1] => and )
> [13] => Array ( )
> [14] => Array ( )
> [15] => Array ( )
> [16] => Array ( [0] => the [1] => the )
> [17] => Array ( )
> [18] => Array ( [0] => and [1] => and )
> [19] => Array ( )
> [20] => Array ( )
>
> So how do you interpret that a sort of instant 2D array.
What that tells me is that the 7, 8, 9, 13, 16, and 18th iterations of
the loop actually matched something with preg_match. The other loops did
not.
You may want to check the result of preg_match before you try to save
the value. Then you won't end up with all of those empty arrays inside
of $matches.
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/