PHP
  Home arrow PHP arrow Page 2 - Working with the Tidy Library in PHP 5
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Working with the Tidy Library in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-06-26

    Table of Contents:
  • Working with the Tidy Library in PHP 5
  • Parsing (X)HTML strings
  • Implementing the tidy_clean_repair() function
  • Using the tidy_parse_file() and tidy_repair_file() functions

  • 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


    Working with the Tidy Library in PHP 5 - Parsing (X)HTML strings


    (Page 2 of 4 )

    As I stated in the beginning of this article, the Tidy library can be really useful in those cases where a specific section of (X)HTML has been badly formatted and, in consequence, it needs to be fixed quickly.

    With reference to performing this code-correcting process, Tidy is packaged with a neat set of format-cleaning functions, starting with the one called "tidy_parse_string()" whose implementation is demonstrated by the example below:

    <?php
    // example of 'tidy_parse_string()' function
    ob_start();
    ?>
    <html>
      <head>
       <title>This file will be parsed by Tidy</title>
      </head>
      <body>
       <p>This is an erroneous line
       <p>This is another erroneous line</i>
      </body>
    </html>
    <?php
    $fileContents=ob_get_clean();
    $params=array('indent'=>TRUE,'output-xhtml'=>TRUE,'wrap'=>200);
    $tidy=tidy_parse_string($fileContents,$params,'UTF8');
    $tidy->cleanRepair();
    echo $tidy;

    /* 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>
        <title>
            This file will be parsed by Tidy
        </title>
      </head>
      <body>
        <p>This is an erroneous line</p>
        <p>This is another erroneous line</p>
      </body>
    </html>
    */

    As you can see, there are some important things to note with reference to the above example. First, I used the previously mentioned "tidy_parse_string()" function along with a few simple formatting input parameters to logically parse a specified (X)HTML string. In this case, the string in question has been placed into an output buffer, and then interpreted. This condition can be easily modified, however -- for instance, to read the respective data via a native PHP function.

    Besides, you should notice that the "tidy_parse_string()" function returns to client code a new "Tidy" object, which has a bunch of methods and properties that can be really useful to perform a variety of tasks, including the correction of missing and erroneous tags. The previous example shows how to format properly the prior (X)HTML string via the "cleanRepair()" method.

    And finally, you can see that the sample string has been fixed, not only by correcting its erroneous <p> and </li> tags, but adding on top of it a DTD statement. Undoubtedly, after studying the previous code sample, you'll have to agree with me that using the Tidy extension with PHP 5 is indeed a no-brainer process, right?

    Okay, at this point I showed you how to use the "tidy_parse_string()" function, with the purpose of parsing and formatting correctly some basic (X)HTML markup. However, Tidy has another function called "tidy_clean_repair()," which as you'll see in a moment, can also be helpful for repairing badly-formatted (X)HTML strings.

    To learn how this brand new Tidy function will be implemented, please jump into the following lines and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In case you didn't know, it's possible to access the main fatures of the popular...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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