DomXML Example
This short example shows how to use the DomXML extension.
To try this example you need to have the DomXML extension enabled.
// example HTML code: (could also come from an URL) $html = '<html> <head> <title>links</title> </head> <body> <a href="link1.htm" title="Link title 1" target="_blank">Link #1</a><br/> <a href="link2.htm" title="Link title 2" target="_blank">Link #2</a><br/> <a href="link3.htm" title="Link title 3" target="_blank">Link #3</a><br/> </body> </html>'; // check if DomXML is available: if (!function_exists('DomDocument')){ die('DomXML extension is not available :-('); } print '<pre>'; // create new DOM object: $dom = new DomDocument(); // load HTML code: $dom->loadHtml($html); // get tags by tagname (all <a> tags / links): $tags = $dom->getElementsByTagName('a'); // loop trough all links: foreach ($tags as $a){ print '<b>' . $a->nodeValue . '</b><br/>'; // does this tag have attributes: if ($a->hasAttributes()){ // loop trough all attributes: foreach ($a->attributes as $attribute){ print '- ' . $attribute->name . ': ' . $attribute->value; print "<br/>"; } } print "<hr/>"; } print '</pre>';
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
Jonas August 13, 2007 at 23:08
Thanks for the comment Guide!
Guide gratuit August 13, 2007 at 13:29
Just a complement for some people: i had an error with the loadhtml function; my hosting is with php4 and php5. To resolve, i named the file with .php5 instead of .php and it works!