<?php
// Using DOM to fix sloppy HTML.
// An example by Tyson Clugg <tyson@clugg.net>
//
// vim: syntax=php expandtab tabstop=2
function tidyHTML($buffer)
{
// load our document into a DOM object
$dom = @DOMDocument::loadHTML($buffer);
// we want nice output
$dom->formatOutput = true;
return($dom->saveHTML());
}
// start output buffering, using our nice
// callback funtion to format the output.
ob_start("tidyHTML");
?>
<html>
<p>It's like comparing apples to oranges.
</html>
<?php
// this will be called implicitly, but we'll
// call it manually to illustrate the point.
ob_end_flush();
?>
The above code takes out sloppy HTML:
<html>
<p>It's like comparing apples to oranges.
</html>
And cleans it up to the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>It's like comparing apples to oranges.
</p></body></html>
DOMDocument->saveHTML()
(No version information available, might be only in CVS)
DOMDocument->saveHTML() — Dumps the internal document into a string using HTML formatting
Popis
class DOMDocument {string saveHTML ( void )
}
Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.
Návratové hodnoty
Returns the HTML, or FALSE if an error occurred.
Příklady
Příklad 400. Saving a HTML tree into a string
<?php
$doc = new DOMDocument('1.0');
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$title = $doc->createElement('title');
$title = $head->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo $doc->saveHTML();
?>
Viz také
| DOMDocument->saveHTMLFile() |
| DOMDocument->loadHTML() |
| DOMDocument->loadHTMLFile() |
DOMDocument->saveHTML()
tyson at clugg dot net
21-Apr-2005 05:44
21-Apr-2005 05:44
