To avoid temporary insanity, it's worth noting that if you are pushing HTML through tidy with the show-body-only option set to TRUE, then calling tidy->root() will return the HTML including doctype, html, head, title tags etc.
To get your body only HTML, do the usual thing of casting the tidy object as a string.
<?php
//assume $config and $encoding are set for this example
$markup = "<h2>Welc<foo>ome</h2>";
$tidy = new Tidy;
$tidy->parseString($markup, $config, $encoding);
$tidy->cleanRepair();
print (string)$tidy->root();
/**
result:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<h2>Welcome</h2>
</body>
</html>
*/
print (string)$tidy;
/**
result:
<h2>Welcome</h2>
*/
?>
tidy_get_root
(PHP 5, PECL tidy:0.5.2-1.0)
tidy_get_root — Returns a tidyNode object representing the root of the tidy parse tree
Description
Procedural style:
tidyNode tidy_get_root
( tidy $object
)
Object oriented style:
tidyNode tidy->root
( void
)
Returns a tidyNode object representing the root of the tidy parse tree.
Example #1 dump nodes
<?php
$html = <<< HTML
<html><body>
<p>paragraph</p>
<br/>
</body></html>
HTML;
$tidy = tidy_parse_string($html);
dump_nodes($tidy->root(), 1);
function dump_nodes($node, $indent) {
if($node->hasChildren()) {
foreach($node->child as $child) {
echo str_repeat('.', $indent*2) . ($child->name ? $child->name : '"'.$child->value.'"'). "\n";
dump_nodes($child, $indent+1);
}
}
}
?>
Примерът по-горе ще изведе:
..html ....head ......title ....body ......p ........"paragraph" ......br
Забележка: Тази функция е налична единствено със Zend Engine 2, т.е. PHP >= 5.0.0.
tidy_get_root
james dot ellis at gmail dot com
11-Dec-2007 11:11
11-Dec-2007 11:11
