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  
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? 
PHP

Working with the Tidy Library in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT