paullb
paullb

Reputation: 4325

Regular Expressions to get rows of a table

I have a table that I am trying to parse

<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>

each row is similarly formatted and I want to split them apart using regular expressions. I have tried everything I can think of but it always seems to take the whole contents as the match

I've tried stuff like this

$pattern = ':(<tr>.*</tr>):';
 preg_match_all( $pattern , $working, &$regs2  );

but it always maximally takes everything in one go rather than minimally taking it row by row.

This is probably pretty basic but I just can't seen to get it.

Upvotes: 3

Views: 3648

Answers (3)

ShaneTheKing
ShaneTheKing

Reputation: 725

In the regex tester I usually use, it seems to work normally. (http://regexpal.com/) If it seems like it's too greedy, try using a ? after the * to calm it down a bit. If you're not wanting the capture the <tr></tr> move the () to the inside, like <tr>(.*?)</tr>/

Upvotes: 2

Rafe Kettler
Rafe Kettler

Reputation: 76945

You need to make the .* pattern non-greedy by adding a ?. Try .*? as the middle pattern and see if the problem persists.

Really, you shouldn't use regex to parse HTML, but you did ask what was wrong, so...

Upvotes: 3

dm03514
dm03514

Reputation: 55922

http://simplehtmldom.sourceforge.net/ Use Simple HTML DOm, it will make parsing the table quite easy

Upvotes: 1

Related Questions