$HTML=
&!doctype html public“-//w3c//dtd xhtml 1.0严格//en”
“http://www.w3.org/tr/xhtml1/dtd/xhtml1 strict.dtd”>
&头;
phpro.org和/头& GT;
和身体;
周六预测
&!--2008年5月23日星期五8时28分发布-->
TSBOR>
T>
悉尼19°好。多半阳光明媚。& LT/TR & GT;
T>
墨尔本16°雾然后很好。& LT/TR & GT;
T>
布里斯班24°大部分都很好& LT/TR & GT;
T>
珀斯21°淋浴次数少,稍后增加。& LT/TR & GT;
T>
阿德莱德20°好。多半阳光明媚。& LT/TR & GT;
T>
霍巴特13°主要是好的。& LT/TR & GT;
T>
堪培拉15°很好,大部分是晴天。& LT/TR & GT;
T>
达尔文32°阳光明媚。& LT/TR & GT;
&T/BOT>
&表;
和/身体;
& lt//html & gt;
;
/***新的DOM对象***/
$dom=新建domdocument;
/***将HTML加载到对象中***/
$dom->加载HTML($html);
/***放弃空白***/
$dom->preserveWhitespace=false;
/***表的标签名***/
$tables=$dom->getElementsByTagname('table');
/***从表中获取所有行***/
$rows=$tables->项(0)->getElementsByTagname('tr');
/***循环表行***/
foreach($rows作为$row)
{
/***按标记名获取每列***/
$cols=$row->getElementsByTagname(“td”);
/***返回值***/
echo$cols->项(0)->节点值。'
';
echo$cols->项(1)->节点值。'
';
echo$cols->项(2)->节点值;
echo'
';
}
?gt;
< /代码>
出http://www.php.net/manual/en/book.dom.php
以下是由提供的示例PHP Pro
$html = '
/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
PHPRO.ORGForecast for Saturday
Sydney | 19° | Fine. Mostly sunny. |
Melbourne | 16° | Fog then fine. |
Brisbane | 24° | Mostly fine |
Perth | 21° | Few showers, increasing later. |
Adelaide | 20° | Fine. Mostly sunny. |
Hobart | 13° | Mainly fine. |
Canberra | 15° | Fine, mostly sunny. |
Darwin | 32° | Fine and sunny. |
';
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
$dom->loadHTML($html);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('table');
/*** get all rows from the table ***/
$rows = $tables->item(0)->getElementsByTagName('tr');
/*** loop over the table rows ***/
foreach ($rows as $row)
{
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('td');
/*** echo the values ***/
echo $cols->item(0)->nodeValue.'
';
echo $cols->item(1)->nodeValue.'
';
echo $cols->item(2)->nodeValue;
echo '
';
}
?>