I've been playing around with some code, adapted from the code SilverBulletUK gave me in a previous post, hoping to load an HTML document into a DOMDocument object.
According to the manual, "Unlike loading XML, HTML does not have to be well-formed to load." -- however, loading this page using the LoadHTMLFile function is throwing the following warning:
Here is my code:Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: Opening and ending tag mismatch: td and font in http://news.google.com/news?ie=utf8&...gs&hl=en&gl=us, line: 25 in /home/.../test4.php on line 5
Furthermore, the $aLinkHrefs array is empty despite there being two <link> elements with href attributes in the source for the page I'm loading.PHP Code:<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$oDOMDoc = new DOMDocument();
$oDOMDoc->loadHTMLFile($_POST['url']);
$oNodeList = $oDOMDoc->getElementsByTagName('link')
or die("No Elements Found");
$aLinkHrefs = array();
foreach ($oNodeList as $oLinkNode)
{
if( $oLinkNode->hasAttribute('href') )
{
if( strlen($oLinkNode->getAttribute('href')) > 0 )
{
$aLinkHrefs[] = $oLinkNode->getAttribute('href');
}
}
}
}
echo '<form action="" method="post">';
echo '<input type="text" name="url" id="url" style="width:700px;" />';
echo '<input type="submit" value="Submit" />';
echo '<textarea style="width:800px;height:600px;display:block;">';
if(is_array($aLinkHrefs)) { print_r($aLinkHrefs); }
echo '</textarea>';
echo '</form>';
?>
Any help would be much appreciated![]()











Bookmarks