Speed up development with full-stack environments for every branch.

Learn More

How to create a simple HTML document with DOMDocument class [PHP]

28 Runs 87 Views 20 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * PHP DOM module
 * Creating a simple HTML document with DOMDocument class
 * 
 */
//create a new document
$doc = new DOMDocument('1.0');

//create and append html elements to the document
$root = $doc->createElement('html');
$doc->appendChild($root);

$head = $doc->createElement('head');
$root->appendChild($head);

$title = $doc->createElement('title');
$head->appendChild($title);

$text = $doc->createTextNode('This is the title');
$title->appendChild($text);

$body = $doc->createElement('body');
$root->appendChild($body);

$bodyContent = $doc->createTextNode('THIS IS THE BODY CONTENT');
$body->appendChild($bodyContent);

//format the output for nice display
$doc->formatOutput = true;

//to see the full output of the document 
echo '<center><pre>' . htmlentities($doc->saveHTML(), ENT_COMPAT | ENT_HTML401, "ISO-8859-1") . '</pre></center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).