I have problem with parsing HTML with DOM in PHP. I want to retrieve href value but giving me error.
I want row value and href value together in a two dimensional array. The last line in code also give error too. Any Idea ? The output I want is --
1,"http://.....",User
2,"http://..... ",Server ...etc in 2D array.
<html>
<body>
<table>
<tbody>
<tr>
<td>1 </td>
<td><a href="http://www.abcd.net"></a></td>
<td>User</td>
</tr>
<tr>
<td>2 </td>
<td><a href="http://www.def.net"></a></td>
<td>Server</td>
</tr>
</tbody>
</table>
</body>
</html>
Here is PHP Code
$resArr = array();
$dom = new domDocument;
@$dom -> loadHTML(file_get_contents($link));
$dom -> preserveWhiteSpace = false;
$linkt = $dom -> getElementsByTagName('table');
$linkt1 = $linkt -> item(2);
//tr
foreach ($linkt1 -> childNodes as $key => $tag){
//td
foreach ($tag -> childNodes as $key1 => $tag1){
foreach ($tag1 -> childNodes as $key2 => $tag2){
echo $tag2->hasattribute('href');
//Error Occur here ----Fatal error: Call to
//undefined method DOMText::hasattribute() in on line 38
}
}
}
$resArr[$i][0] = $tag -> childNodes -> item(0) -> nodeValue;
$resArr[$i][3] = $tag -> childNodes -> item(3) -> nodeValue;
$resArr[$i][1] = $tag1 -> childNodes -> item(1) ->
childNodes -> item(0) -> getAttribute('href'); //the same error as above