Skip to content

Instantly share code, notes, and snippets.

@dphiffer
Last active August 29, 2015 14:11
Show Gist options
  • Save dphiffer/78a7e69f310836a75a19 to your computer and use it in GitHub Desktop.
Save dphiffer/78a7e69f310836a75a19 to your computer and use it in GitHub Desktop.
Test case for PHP Simple HTML DOM Parser vs. native DomDocument
<?php
$test = <<<END
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test case</title>
</head>
<body>
<img alt="foo" src="foo.jpg">
<img alt="bar"src="bar.jpg">
</body>
</html>
END;
require 'simplehtmldom_1_5/simple_html_dom.php';
$html = new simple_html_dom();
$html->load($test);
echo '<pre>';
foreach ($html->find('img') as $img) {
$tag = htmlentities($img->outertext);
echo "$tag\n";
}
/*
Output:
<img alt="foo" src="foo.jpg">
<img alt="bar">
*/
?>
<?php
$test = <<<END
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test case</title>
</head>
<body>
<img alt="foo" src="foo.jpg">
<img alt="bar"src="bar.jpg">
</body>
</html>
END;
$dom = new DOMDocument();
$dom->loadHTML($test);
echo '<pre>';
foreach ($dom->getElementsByTagName('img') as $img) {
$tag = htmlentities($dom->saveHtml($img));
echo "$tag\n";
}
/*
Output:
<img alt="foo" src="foo.jpg">
<img alt="bar" src="bar.jpg">
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment