This question already has an answer here:
I have a table:
<table class="table_class" >
<tr>
<td>key</td>
<td>value</td>
</tr>
</table>
The table may have any count of <tr>
I have regexp:
<table class="table_class">(<tr.*?><td>(.*?)</td><td>(.*?)</td></tr>){1,}</table>
But matches array contains only last match.
Just (<tr.*?><td>(.*?)</td><td>(.*?)</td></tr>)
I can not do, because other table will may be.
Before apply preg_match_all I delete whitespaces. How do this? Thanks!
UPD: example with a few tables
<table>
<tr>
<td>key</td>
<td>value</td>
</tr>
</table>
<table class="table_class" >
<tr>
<td>key</td>
<td>value</td>
</tr>
</table>
yet, I will want to know why my regexp match only last <tr>
))
</div>
Now usually I'm first to say it's fine to use regexps to extract data from HTML occasionally, as it's oft just faster and more efficient to do so than using a real parser. This is not one of those cases as the structure of the HTML is more than relevant.
Instead consider something like this:
$doc = DOMDocument::loadHTML(<<<HTML
<table class="table_class" >
<tr><td>key1</td><td>value1</td></tr>
<tr><td>key2</td><td>value2</td></tr>
<tr><td>key3</td><td>value3</td></tr>
<tr><td>key4</td><td>value4</td></tr>
</table>
HTML
);
foreach($doc->getElementsByTagName('tr') as $row) {
foreach($row->getElementsByTagName('td') as $cell)
var_dump($cell->nodeValue);
}