PHP
  Home arrow PHP arrow Page 2 - 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 Rational Software Development Conference
 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Handling HTML Strings and Files with the DOM XML Extension in PHP 5 - Loading HTML code from a specific string with the loadHTML() method
    (Page 2 of 4 )

    In consonance with the concepts that I deployed in the introduction, the DOM XML extension includes some useful methods aimed specifically at handling HTML in different ways. To demonstrate this specific capacity of the library I’m going to teach you how to utilize the brand new “loadHTML()” method, which comes in handy when loading an HTML string onto the web server’s memory.

    That being said, here’s an example that illustrates how the aforementioned method does its thing:


    // example on loading HTML from an existing string using the 'loadHTML()' method


    $html='<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-
    Type" content="text/html; charset=iso-8859-1" /><title>Example of loading HTML</title></head><body><p>Example on loading HTML</p></body></html>';

    $dom=new DOMDocument();

    $dom->loadHTML($html);

    echo $dom->saveHTML();


    /* displays the following

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

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

    <head>

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

    <title>Example of loading HTML</title>

    </head>

    <body><p>Example on loading HTML</p></body>

    </html>

    */


    As demonstrated by the above code sample, the “loadHTML()” method is convenient for loading an HTML string into a PHP object. In this particular case, the method in question is first used to retrieve some simple HTML content, which is then echoed to the browser. Not too hard to understand, right?

    So far, so good. With the introduction of the previous hands-on example, hopefully you’ve already grasped how the pertinent “loadHTML()” method does its business. However, the DOM XML extension still has some other helpful methods that can be used to handle HTML. Thus, since you may want to learn how to put them to work for you, in the upcoming section I’ll be teaching you a brand new method, named “loadHTMLFile(), which can be used to read the contents of a specified HTML file.

    To learn the full details of how to use this handy method, please click on the link that appears below and keep reading.

    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

    - Setting Up a Web-based Image Hosting Service
    - 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

     
    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 6 hosted by Hostway