PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Implementing the tidy_clean_repair() function
    ( Page 3 of 4 )

    As I stated previously, the Tidy library comes equipped with another useful function, named "tidy_clean_repair()," which behaves in a manner nearly identical to the "cleanRepair()" method demonstrated in the section that you just read. In this case, this brand new function will fix any badly-formatted (X)HTML markup, and its usage is illustrated by the following example:

    // example of 'tidy_clean_repair()' 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>';
    $tidy=tidy_parse_string($html);
    tidy_clean_repair($tidy);
    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, using the above "tidy_clean_repair()" function is indeed a very straightforward process, since the function in question performs a clean-up task on a specified (X)HTML string, certainly behaving identically to its cousin "cleanRepair()" method.

    Additionally, when it comes to correcting the format of a specific (X)HTML string, the Tidy library also offers the neat "tidy_repair_string()" function, which can be used as indicated below:

    // example of 'tidy_repair_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();
    $tidy=tidy_repair_string($fileContents);
    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>
    */

    So far, so good, right?. At this point you've hopefully learned how to use a few useful functions included with the Tidy library to format correctly a particular (X)HTML string. Nevertheless, as you might have guessed, Tidy has plenty of neat functions when it comes to fixing badly-formatted markup.

    Thus, keeping in mind this important fact, in the section to come I'm going to show you how to use the excellent functionality provided by the Tidy extension to parse, and eventually correct, the format of different (X)HTML files.

    As you know, this brand new Tidy feature will be covered in the next few lines, so 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek