使用dom html。我想將節點值轉換爲字符串:將nodevalue轉換爲字符串
$html = @$dom->loadHTMLFile('url');
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('body');
$rows = $tables->item(0)->getElementsByTagName('tr');
// loop over the table rows
foreach ($rows as $text =>$row)
{
$t=1;
// get each column by tag name
$cols = $row->getElementsByTagName('td');
//getting values
$rr = @$cols->item(0)->nodeValue;
print $rr; (it prints values of all 'td' tag fine)
}
print $rr; (it prints nothing) I want it to print here
?>
我想要nodevalues轉換爲字符串進一步操作。
歡迎來到SO。請花點時間讓你的問題有一點可讀性,這樣我們可以幫助你更好。 – Starx 2012-01-06 08:00:39
您正在使用'@'運算符來抑制錯誤。如果發生錯誤,您將不會注意到它。例如。如果文檔無法加載(服務器關閉),則不會有節點提取任何內容。另外,如果你正確地縮進你的代碼,它會更好的可讀性,並且更容易處理。 – hakre 2012-01-06 08:07:28