PHP
  Home arrow PHP arrow Page 4 - Handling HTML Strings and Files with t...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Handling HTML Strings and Files with the DOM XML Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-25

    Table of Contents:
  • Handling HTML Strings and Files with the DOM XML Extension in PHP 5
  • Loading HTML code from a specific string with the loadHTML() method
  • Reading contents from HTML files with the loadHTMLFile() method
  • Building HTML files from their DOM representation with the saveHTMLFile() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Handling HTML Strings and Files with the DOM XML Extension in PHP 5 - Building HTML files from their DOM representation with the saveHTMLFile() method
    (Page 4 of 4 )


    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! 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The DOM XML PHP extension has a few additional methods that can be used to process...
     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway