PHP
  Home arrow PHP arrow Page 3 - 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 
 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

    Virtual Tradeshows by Ziff Davis Enterprise – A Unique Opportunity to Interact with IT Experts, Access Information, and Gain Insight on Today’s Trends in Technology Learn more

    Handling HTML Strings and Files with the DOM XML Extension in PHP 5 - Reading contents from HTML files with the loadHTMLFile() method
    (Page 3 of 4 )

    As I stated in the previous section, the DOM XML library comes equipped with another useful method that can be used to read the contents of a specified HTML file, which can then be easily processed later on.

    The method that performs such a useful task is called “loadHTMLFile()” and can be implemented in the following way:


    (definition of ‘sample_file.htm’ file)


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>This is a sample HTML file</title>

    </head>

    <body>

    <p>This is a sample HTML file</p>

    </body>

    </html>



    // example on loading HTML from an existing file using the 'loadHTMLFile()' method


    $dom=new DOMDocument();

    // load HTML from existing file

    $dom->loadHTMLFile('sample_file.htm');

    echo $dom->saveHTML();


    /* displays the following

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title>This is a sample HTML file</title>

    </head>

    <body>

    <p>This is a sample HTML file</p>

    </body>

    </html>

    */


    As you can see, I first defined a primitive HTML file, called “sample_file.htm”, and then coded a simple script that is responsible for using the aforementioned “loadHTMLFile()” method to read the corresponding file content. In the end, the script finishes its execution by displaying this content on the browser.

    After looking at the previous hands-on example, you’ll have to agree with me that using the DOM XML API to read the content from a few specified HTML files is actually a no-brainer process that can be tackled with minor troubles.

    All right, you learned a few additional methods that come packaged with the DOM XML extension aimed at handling HTML in different ways. Of course, the functionality of these methods is rather limited, but they do perform decently when it comes to working with basic HTML strings and files.

    However, the DOM XML library provides you with yet another HTML-related method that can potentially be useful, particularly in those cases where you need to build an HTML file from its corresponding DOM representation. This sounds interesting, right? Assuming that this topic has caught your attention, in the last section of this tutorial, I’m going to show you how to create HTML files using the DOM API.

    To see how this will be achieved, please jump ahead and read the next few lines.

    More PHP Articles
    More By Alejandro Gervasio


       · 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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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