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 — Tidy パースツリーのルートを表す tidyNode を返す
説明
手続き型:
tidyNode tidy_get_root
( tidy $object
)
オブジェクト指向型:
tidyNode tidy->root
( void
)
Tidy パースツリーのルートを表す tidyNode を返します。
例1 ノードをダンプする
<?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
