PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument::saveHTMLFile> <DOMDocument::save
Last updated: Fri, 18 Jul 2008

view this page in

DOMDocument::saveHTML

(No version information available, might be only in CVS)

DOMDocument::saveHTML — Dumps the internal document into a string using HTML formatting

Description

string DOMDocument::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.

Return Values

Returns the HTML, or FALSE if an error occurred.

Examples

Example #1 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();

?>



DOMDocument::saveHTMLFile> <DOMDocument::save
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
DOMDocument::saveHTML
Anonymous
26-Apr-2008 04:15
function getDOMString($retNode) {
        if (!$retNode) return null;
        $retval = strtr($retNode-->ownerDocument->saveXML($retNode),
        array(
            '></area>' => ' />',
            '></base>' => ' />',
            '></basefont>' => ' />',
            '></br>' => ' />',
            '></col>' => ' />',
            '></frame>' => ' />',
            '></hr>' => ' />',
            '></img>' => ' />',
            '></input>' => ' />',
            '></isindex>' => ' />',
            '></link>' => ' />',
            '></meta>' => ' />',
            '></param>' => ' />',
            'default:' => '',
            // sometimes, you have to decode entities too...
            '&quot;' => '&#34;',
            '&amp;' =>  '&#38;',
            '&apos;' => '&#39;',
            '&lt;' =>   '&#60;',
            '&gt;' =>   '&#62;',
            '&nbsp;' => '&#160;',
            '&copy;' => '&#169;',
            '&laquo;' => '&#171;',
            '&reg;' =>   '&#174;',
            '&raquo;' => '&#187;',
            '&trade;' => '&#8482;'
        ));
        return $retval;
    }
mjaque at ilkebenson dot com
20-Feb-2008 09:48
Regarding last note, you can post-process saveXML string in order to close empty tags with the following function:

<?php
   
function cerrarTag($tag, $xml){
       
$indice = 0;
        while (
$indice< strlen($xml)){
           
$pos = strpos($xml, "<$tag ", $indice);
            if (
$pos){
               
$posCierre = strpos($xml, ">", $pos);
                if (
$xml[$posCierre-1] == "/"){
                   
$xml = substr_replace($xml, "></$tag>", $posCierre-1, 2);
                }
               
$indice = $posCierre;
            }
            else break;
        }
        return
$xml;
    }
?>

At least script and select empty elements should be closed. This example shows how it can be used:

<?php
    define
("CABECERA_XHTML", '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');

 
$xhtml = $docXML->saveXML($docXML->documentElement);
 
$xhtml = cerrarTag("script", $xhtml);
 
$xhtml = cerrarTag("select", $xhtml);
 
$xhtml = CABECERA_XHTML."\n".$xhtml;
  echo
$xhtml;
?>
mjaque at ilkebenson dot com
19-Feb-2008 07:34
DOMDocument->saveXML() doesn't generate a proper XHTML format either.

There is a problem with "script" empty elements. For example:

This will be the code generated by saveXML, with an empty script tag.

<html>
  <head>
    <script type="text/JavaScript" src="myScript.js"/>
  </head>
  <body>
    <p>I will not appear</p>
    <script type="text/JavaScript">
    alert("Not working");
    </script>
  </body>
</html>

I don't know if this is valid XHTML (W3C Validator doesn't complain), but both FF 2.0 and IE 6 will not render it properly. Both will use </script> as the closing tag for the first script causing js errors and ignoring in between elements.
archanglmr at yahoo dot com
27-Nov-2007 11:28
If created your DOMDocument object using loadHTML() (where the source is from another site) and want to pass your changes back to the browser you should make sure the HTTP Content-Type header matches your meta content-type tags value because modern browsers seem to ignore the meta tag and trust just the HTTP header. For example if you're reading an ISO-8859-1 document and your web server is claiming UTF-8 you need to correct it using the header() function.

<?php
header
('Content-Type: text/html; charset=iso-8859-1');
?>
xoplqox
20-Nov-2007 07:07
XHTML:

If the output is XHTML use the function saveXML().

Output example for saveHTML:

<select name="pet" size="3" multiple>
    <option selected>mouse</option>
    <option>bird</option>
    <option>cat</option>
</select>

XHTML conform output using saveXML:

<select name="pet" size="3" multiple="multiple">
    <option selected="selected">mouse</option>
    <option>bird</option>
    <option>cat</option>
</select>
tyson at clugg dot net
22-Apr-2005 01:44
<?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::saveHTMLFile> <DOMDocument::save
Last updated: Fri, 18 Jul 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites