Home arrow PHP arrow Page 4 - Handling HTML Strings and Files with the DOM XML Extension in PHP 5

Building HTML files from their DOM representation with the saveHTMLFile() method - PHP

The DOM XML extension has a few additional methods that can be used to process HTML files and strings, at least at a pretty basic level. Thus, this fifth part of the series will be entirely focused on explaining how to work with these methods. I will include some illustrative code samples.

TABLE OF CONTENTS:
  1. Handling HTML Strings and Files with the DOM XML Extension in PHP 5
  2. Loading HTML code from a specific string with the loadHTML() method
  3. Reading contents from HTML files with the loadHTMLFile() method
  4. Building HTML files from their DOM representation with the saveHTMLFile() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 25, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement


As I mentioned in the section that you just read, the last example that I’m going to teach you in this article will be specifically aimed at demonstrating how to build a basic HTML file from its corresponding DOM representation. Surely, at this very moment, you’re wondering how this process can be performed. The answer to that question is via the brand new “saveHTMLFile()” method.

In order to help you grasp how this method functions, below I coded another illustrative example that shows how to create a primitive HTML document via the DOM API and then save it to a specified file destination.

Here’s the corresponding code sample. Please take a close look at it: 

// example on building an HTML document from the DOM representation using the saveHTMLFile()' method


$dom=new DOMDocument('1.0');

// format output

$dom->formatOutput=true;

// create <html> element

$root=$dom->createElement('html');

$root=$dom->appendChild($root);

// create <head> element

$head=$dom->createElement('head');

$head=$root->appendChild($head);

// create <title> element

$title=$dom->createElement('title');

$title=$head->appendChild($title);

// create text for title of HTML page

$text=$dom->createTextNode('This title was created with the DOM XML extension of PHP');

$text=$title->appendChild($text);

// save HTML to file

echo 'The file just created has a size of '. $dom->saveHTMLFile('test_file.htm'). ' bytes.';


/* displays the following

The file just created has a size of 168 bytes

*/


As shown in the previous example, a trivial HTML document is built by using some of the methods that you learned so far, such as “createElement()” and “appendChild()” respectively. Then, once the document in question has been created, it’s simply saved to a target file, called “test_file.htm”, by calling the pertinent “saveHTMLFile()” method.

While the functionality of “saveHTMLFile()” is pretty easy to grasp, you should notice two important things. First, I used the brand new property, called “formatOutput”, to adequately format the contents of the file that is just about to be created. And second, if the mentioned file doesn’t exist, then the “saveHTMLFile()” method will attempt to build it. Simple and short!

Lastly, before I forget, here’s the signature of the sample HTML file constructed with “saveHTMLFile()”:


<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>This title was created with the DOM XML extension of PHP</title>

</head></html>


As usual with my articles on PHP development, you’re free to tweak all of the code samples developed earlier so you can improve your skills using the DOM XML PHP extension.

Final thoughts

In this fifth chapter of the series, you learned how to handle HTML strings and files through the set of methods provided by the DOM XML extension. Since they’re quite simple to use, you shouldn’t have major difficulties incorporating them into your own PHP applications.

In the upcoming part, I’ll be exploring the solid capabilities of the DOM XML library when it comes to parsing parent and child nodes of a specified XML document, so you don’t have any excuses to miss it! 



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: