preg_match_all help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

preg_match_all help

Post 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:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: preg_match_all help

Post 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);
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: preg_match_all help

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: preg_match_all help

Post 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).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: preg_match_all help

Post 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 ...
Last edited by VladSun on Tue Jan 22, 2008 8:11 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: preg_match_all help

Post by Chris Corbyn »

It's not preg_match() specifically, it's PCRE (i.e. perl). Just the way it's always worked I guess :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: preg_match_all help

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: preg_match_all help

Post by Chris Corbyn »

I concede :) I just tried various different things and obviously my memory has failed me from my perl days :P
Post Reply