Page 1 of 1

preg_match_all help

Posted: Mon Jan 21, 2008 11:52 pm
by SidewinderX
I'm no genius when it comes to regex, so I would appreciate a little help.

Code: Select all

<?php
$content = '<td><p>501</p></td>
<td><p>2,633</p></td>
<td><p>2,398</p></td>
<td><p>1.1</p></td>
<td><p>12</p></td>
<td><p>373</p></td>
<td><p>147</p></td>
<td><p><img src="img.png" /></p></p></td>';
                        
preg_match_all('/<td><p>(.*?)<\/p><\/td>/', $content, $matches);
print_r($matches);
?>
Which prints this:
Array
(
[0] => Array
(
[0] => <td><p>501</p></td>
[1] => <td><p>2,633</p></td>
[2] => <td><p>2,398</p></td>
[3] => <td><p>1.1</p></td>
[4] => <td><p>12</p></td>

[5] => <td><p>373</p></td>
[6] => <td><p>147</p></td>
[7] => <td><p><img src="img.png" /></p></p></td>
)

[1] => Array
(
[0] => 501
[1] => 2,633
[2] => 2,398
[3] => 1.1
[4] => 12
[5] => 373
[6] => 147
[7] => <img src="img.png" /></p>
)

)
I just want this:
Array
(
[0] => Array
(
[0] => 501
[1] => 2,633
[2] => 2,398
[3] => 1.1
[4] => 12
[5] => 373
[6] => 147
[7] => <img src="img.png" /></p>
)
)
All help is appreciated.
Thanks.

edit:
meh...forgot there was a regex forum for these questions :banghead:

Re: preg_match_all help

Posted: Tue Jan 22, 2008 12:10 am
by Chris Corbyn
Tricky... I mean, I'd probably just take array($matches[1]) and be done with it (it's the same). But anyway:

Code: Select all

preg_match_all('/(?<=<td><p>).*?(?=<\/p><\/td>)/', $content, $matches);

Re: preg_match_all help

Posted: Tue Jan 22, 2008 2:46 am
by VladSun
preg_match_all will always return the full matches - there is no way to make it not to do it.
I hate it a little because of that.

The only thing I could found about not returning matches is:

Code: Select all

/(?:pattern(subbpattern))/
In this case only subpattern matches will be returned.

Re: preg_match_all help

Posted: Tue Jan 22, 2008 4:33 am
by Chris Corbyn
VladSun, my code will work because the full match doesn't include the bits before and after (they're lookahead and lookbehind in my pattern).

Re: preg_match_all help

Posted: Tue Jan 22, 2008 5:13 am
by VladSun
Sorry, I didnt look at you regexp :(
Yes, this should work, but I've always wondered why pregmatch would return the whole match - if I wanted it to do so, I would put () surrounding the whole pattern :(
Your regexp will work, but why it has to be so complicated ...

Re: preg_match_all help

Posted: Tue Jan 22, 2008 5:27 am
by Chris Corbyn
It's not preg_match() specifically, it's PCRE (i.e. perl). Just the way it's always worked I guess :)

Re: preg_match_all help

Posted: Tue Jan 22, 2008 5:40 am
by VladSun
I couldn't reproduce it in Perl:

Code: Select all

#/usr/bin/perl $s = "anything that could be here";$s =~ m/anything (.+) could/; print $s."\n";print $_."\n";print $0."\n";print $1."\n"; 
output:

Code: Select all

~# perl 1.pl
anything that could be here
 
1.pl
that

Re: preg_match_all help

Posted: Tue Jan 22, 2008 5:57 am
by Chris Corbyn
I concede :) I just tried various different things and obviously my memory has failed me from my perl days :P