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
SidewinderX
Forum Contributor
Posts: 407 Joined: Fri Jul 16, 2004 9:04 pm
Location: NY
Post
by SidewinderX » Mon Jan 21, 2008 11:52 pm
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Jan 22, 2008 12:10 am
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);
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Jan 22, 2008 2:46 am
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:
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Jan 22, 2008 4:33 am
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).
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Jan 22, 2008 5:13 am
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Jan 22, 2008 5:27 am
It's not preg_match() specifically, it's PCRE (i.e. perl). Just the way it's always worked I guess
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Jan 22, 2008 5:40 am
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Jan 22, 2008 5:57 am
I concede
I just tried various different things and obviously my memory has failed me from my perl days