-1

I have a source page with a table which consists of 15 rows with this content:

<tr class="hlRow" onclick="window.location=link11.href" onmouseover="rowOver(11)" onmouseout="rowOut(11,'#cad9ea')">
  <td class="row3">Latest news</td>
  <td class="row3" id="row_6_11"><a onclick="servOC(11,'/link-to-page.html','',ihTri11)"><img class="tog" id="ihTri11" src="up.png" title="Toggle" height="19" width="19" /></a>14.7w</td>
  <td class="row3" id="name11"><a href="/link-to-page.html" style="float: right; color: green; font-weight: bold;" title="+2 rating, 2 comments">+2<img src="star.png" alt="rating" style="margin-left: 1px;" height="12" width="12" /> 2<img src="bubble.png" alt="comments" style="margin-left: 2px;" height="10" width="10" /></a><a id="link11" href="/link-to-page.html">Got to page</a></td>
  <td class="row3" title="11 files">10 days</td>
  <td class="row3">104</td>
  <td class="row3">108</td>
</tr>

Basically I need to take those rows between <tr> to </tr> tags from source site and show them on mine. I've tried using preg_match_all(), but as my regex experience is very limited I just can't do it properly.

preg_match_all('<tr class="hlRow"(.*?)</td></tr>/i', $turinys, $linkai, PREG_SET_ORDER);
foreach ($linkai as $linkas) {$a1 = $linkas[1]; echo "<table><tr class=\"hlRow\"".$a1."\"></td></tr></table>";}

Even more awesome would be to get only contents from inside of <td> tags and then foreach those on my page.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
DadaB
  • 762
  • 4
  • 12
  • 29
  • [use a DOM parser, fetch the tr with that class and print the outerHTML of that node. No need for regex.](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Jan 06 '12 at 15:09

2 Answers2

0

The way to do this is certainly with the DOM extension rather than with regex. Parsing HTML with regex will drive you insane.

DOM code might look like this...

$dom = new DOMDocument;
$dom->loadHtmlFile('your source url');

$xpath = new DOMXPath($dom);

$rows = $xpath->query('//tr[@class="hlRow"]');

$rowNumber = 1;

foreach ($rows as $row) {
    echo "Row number: ", $rowNumber++, "\n";
    foreach ($row->childNodes as $td) {
        if ($td->nodeName === 'td') {
            echo $td->nodeValue, "\n";
        }
    }
    echo "End of row\n\n";
}
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

To answer the actual question:

You need the /s DOTALL mode for matching across multiple lines with (.*?).

And there is a space between </td> and </tr>, so it would need

 preg_match_all('#<tr class="hlRow"(.*?)</td>\s*</tr>#is'

Also notice the # delimiters, if you are using / as literal characters.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
mario
  • 144,265
  • 20
  • 237
  • 291