找到你要的答案

Q:read html table content with specific class [duplicate]

Q:读与特定类[复制] HTML表格内容

This question already has an answer here:

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?

这个问题在这里已经有了答案:

我有新闻页显示我的新闻。我用表格来显示标题。

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

我有其他表在这页。但是我想把这张桌子拿到另一页。我搜索,只是找到这样:

$text = file_get_contents("http://www.example.com/news");
echo strip_tags($text, "<;表>;<;TR >;<;TH >;<;TD & gt;");

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

answer1: 回答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

我创建了两个表的示例代码。你可以看到输出端

<?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;
?>

这应该requiredtable美元可变打印表

answer2: 回答2:
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>

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

这应该剥去所有的标签,除了那些

echo strip_tags($text, "<;表>;<;TR >;<;TH >;<;TD & gt;");

这会剥去一切,除了绳子:

<;表>;<;TR >;<;TH >;<;TD & gt;

php  html  file-get-contents