1

I have news page that shows my news. I used table to show headlines.

<table class="news">
<tr>
    <th>#</th>
    <th></th>
</tr>
<tr>...</tr>
<tr>...</tr>
</table>

I have other tables in this page. But I want to get this table in another page. I searched and just find this way:

$text = file_get_contents("http://www.example.com/news");
echo strip_tags($text, "<table><tr><th><td>");

Output contains all tables in news page. My goal is just table with class "news".
How can I do that?

PKa
  • 309
  • 2
  • 7
  • 23

2 Answers2

1
echo strip_tags($text, "<table class='news'>|<tr>|<th>|<td>");

This should strip all tags except for those

echo strip_tags($text, "<table><tr><th><td>");

This would strip everything except for the string:

<table><tr><th><td>

DieVeenman
  • 457
  • 1
  • 3
  • 18
1

I have created sample code with two tables. You can see the output at the end

<?php
$html = <<<EOT
<table class="news" border='1'>
<tr>
<th>#</th>
<th></th>
</tr>
<tr><td>New 1 - first </td><td>New 1 - second </td></tr>
<tr><td>New 1 - fifth </td><td>New 1 - forth</td></tr>

</table>
<table class="another_news" border='1'>
<tr>
<th>#</th>
<th></th>
</tr>
<tr><td>Another New 1 - first </td><td>Another New 1 - first </td></tr>
<tr><td>AnotherNew 1 - first </td><td>Another New 1 - first </td></tr>

</table>
EOT;
echo $html;
echo "<hr>";
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your HTML
$xpath = new DOMXPath($doc);
// returns all tables with class news
$tables = $xpath->query('//table[@class="news"]');
$requiredTable = ''; // This will html of tables
foreach ($tables as $table) {
    $requiredTable .=  $doc->saveXML($table);
}
echo $requiredTable;
?>

This should print table in $requiredTable variable

Yogesh
  • 928
  • 1
  • 8
  • 21