Re: preg_match()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jan Reiter wrote:
> Good Afternoon.
> 
>  
> 
> This shouldn't be too complicated, but I can't come up with a solution for
> the problem right now. It's about RegEx in PHP (5.2)
> 
>  
> 
> Is there a way to capture ALL sub elements of an expression like
> preg_match('@a(?[0-9])*b@' ,"a2345678b" , $matches);   ??
> 
> This would produce (below) whereas I'd like to get all the elements (2-8) as
> an array entry each . ([1]=>2, [2]=>3 .)
> 

AFAIK, you would need to know how many digits are there and use that
many capture groups (), because you only have one capture group which
will be overwritten each iteration.  An alternative for your simple
example above would be to do:

preg_match('@a([0-9]+)b@' ,"a2345678b" , $matches);

--then--

print_r(str_split($matches[1]));

To do this multiple times in a large string you would need:

preg_match_all('@a([0-9]+)b@' ,"a2345678b" , $matches);

foreach($matches[1] as $match) {
	print_r(str_split($match));
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux