Home arrow PHP arrow Page 4 - Parsing Web Document Nodes with the Tidy Library in PHP 5

Using the tidy_get_body() and tidy_get_ouput() functions - PHP

Writing well-formatted (X)HTML code to include in the presentation layers of certain PHP applications can be an annoying and time-consuming process for many web developers. However, the Tidy extension that comes integrated with PHP 5 can turn this ugly task into a pleasant experience. Keep reading to learn how.

  1. Parsing Web Document Nodes with the Tidy Library in PHP 5
  2. Parsing and formatting basic (X)HTML code with Tidy
  3. Using the tidy_get_html() and tidy_get_head() functions
  4. Using the tidy_get_body() and tidy_get_ouput() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
July 03, 2007

print this article



In consonance with the concepts expressed in the section that you just read, the last two functions included with the Tidy library that I plan to teach you in this tutorial will be the ones called "tidy_get_body()" and "tidy_get_output()." As you may guess, the first function comes in handy for extracting the <body> section of a concrete (X)HTML string, while the second one simply retrieves the whole string as a unique node.

Now that I have explained how these brand new Tidy functions work, please take a look at the following code samples, which demonstrate their rather limited functionality:

// example on using the 'tidy_get_body()' function

$html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
Tidy</title></head><body><p>Testing Tidy</p></body></html>';
echo $bodyNode->value;

/* displays the following:
  <p>Testing Tidy</p>

// example on using the 'tidy_get_output()' function

$html='<html><head><title>This file will be parsed by
Tidy</title></head><body><p>This is an erroneous line</i>This is
another erroneous line</i></body></html>';
echo tidy_get_output($tidy);

/* displays the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
      This file will be parsed by Tidy
    <p>This is an erroneous line</p>
    <p>This is another erroneous line</p>

As you can see, the source code corresponding to the above examples is very easy to follow. In the first case, the "tidy_get_body()" function is used obviously to retrieve the <body> part of a sample (X)HTML string, certainly a procedure that doesn't bear too much discussion.

With reference to the second code listing, it simply demonstrates how to correct the format of the sample string via the already familiar "cleanRepair()" Tidy method, and then display the respective contents on the browser, in this case using the "tidy_get_output()" function. Quite simple, right?

Finally, as usual with many of my articles on PHP-based web development, feel free to modify the source code of all the examples shown here, if you want to continue exploring how to handle these useful Tidy functions.

Final thoughts

This second article of the series was entirely aimed at demonstrating how to use some simple functions bundled with the Tidy library to extract the different parts of a specified (X)HTML string.

Nevertheless, this story is not yet finished, since in the last tutorial of the series I'm going to show you how to utilize Tidy's remarkable capabilities to keep track of the eventual errors that occur when parsing a web document. You won't want to miss it!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: