-1

我想从其他网站获取一张表格(该表格每天都在更新),并且我希望它显示在我的网站上,只是与原始网站中的排列方式不同。

所以我使用 curl 和 strstr() 获得了表格,但使用字符串函数编辑整个表格似乎毫无意义。我确信有一种更简单的方法可以做到这一点。

也许有一种方法可以创建 xml 格式的原始表,我可以更轻松地使用该结构?

非常感谢!

4

1 回答 1

0

Here's a crude example of how you might consume the table (only to echo out another table), Below I'm using SimpleXML and xpath to search the document for the element that has a class of Table Data and looping through all the TR's and putting some special conditions to deal with the and blocks that exist

Obvious downside is if the third party website's layout changes, your code will stop functioning.

header('Content-Type: text/html; charset=UTF-8');
$html = file_get_contents('http://www.votes-19.gov.il/nationalresults'); // or use curl
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"); 

$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);

echo '<!doctype html><html><head><style type="text/css">body{direction: rtl;}</style></head><body><table>';
if($trxpath = $xml->xpath('//table[@class="TableData"]/tr'))
{
  foreach($trxpath as $tr)
  {
    echo '<tr>';
    echo '<th>' . $tr->th . '</th>';
    foreach($tr->td as $td)
    {
      if($td->div)
        echo '<td>' . $td->div . '</td>';
      else
        echo '<td>' . $td . '</td>';
    }
    echo '</tr>';
  }
}
echo '</table></body></html>';
于 2013-01-24T22:38:14.120 回答