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

Reading contents from HTML files with the loadHTMLFile() 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 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
 

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: