Saturday, November 16, 2024
Google search engine
HomeLanguagesPHP DOMDocument saveHTML() Function

PHP DOMDocument saveHTML() Function

The DOMDocument::saveHTML() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after building the dom document from scratch. 

Syntax:

string DOMDocument::saveHTML( DOMNode $node = NULL )

Parameters: This function accepts single parameter $node which is optional and used to output a subset of the document.

Return Value: This function returns the HTML document on success or FALSE on failure.

Below program illustrates the DOMDocument::saveHTML() function in PHP:

Program: 

php




<?php
 
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0');
 
// Create a root element
$root = $domDocument->createElement('html');
 
// Append the element to the document as root element
$root = $domDocument->appendChild($root);
 
// Create a head element
$head = $domDocument->createElement('head');
 
// Append the element to the document
// as child element
$head = $root->appendChild($head);
 
// Create a title element
$title = $domDocument->createElement('title');
 
// Append the element to the document
// as child element
$title = $head->appendChild($title);
 
// Create a text node
$text = $domDocument->createTextNode(
        'DOMDocument::saveHTML() function');
         
// Add the text node the title element
$text = $title->appendChild($text);
 
// Create a body element
$body = $domDocument->createElement('body');
 
// Append the element to the document
// as child element
$body = $root->appendChild($body);
 
// Create a heading element
$h1 = $domDocument->createElement('h1');
 
// Append the element to the document
$h1 = $body->appendChild($h1);
 
// Create a text node
$text = $domDocument->createTextNode('neveropen');
 
// Add the text node to the heading element
$text = $h1->appendChild($text);
 
// Create a heading element
$h2 = $domDocument->createElement('h2');
 
// Append the element to the document
$h2 = $body->appendChild($h2);
 
// Create a text node
$text = $domDocument->createTextNode(
            'DOMDocument::saveHTML() function');
             
// Add the text node to the heading element
$text = $h2->appendChild($text);
 
// Use saveHTML() function to create
// an HTML document
echo $domDocument->saveHTML();
 
?>


Output:

<html>
<head>
    <title>DOMDocument::saveHTML() function</title>
</head>
<body>
    <h1>neveropen</h1>
    <h2>DOMDocument::saveHTML() function</h2>
</body>
</html>

Reference: https://www.php.net/manual/en/domdocument.savehtml.php

RELATED ARTICLES

Most Popular

Recent Comments