PHP
  Home arrow PHP arrow Page 2 - Handling HTML Strings and Files with the DOM XML Extension in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
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: starstarstarstarstar / 3
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek