PHP
  Home arrow PHP arrow Page 4 - 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 - Using the tidy_parse_file() and tidy_repair_file() functions
    ( Page 4 of 4 )

    In consonance with the concepts deployed in the prior section, you already saw that the Tidy extension comes equipped with a set of functions for parsing and fixing specified (X)HTML strings. In a similar fashion, this powerful PHP package also includes a pair of additional functions, called "tidy_parse_file()" and "tidy_repair_file()" respectively, which are helpful for parsing and correcting badly-formatted (X)HTML files.

    Even when the difference between parsing files or plain strings may seem subtle from a theoretical point of view, in practical terms, it may be rather relevant, particularly in those cases where your PHP applications need to deal with (X)HTML markup stored on text files.

    Having clarified this important point, please pay attention to the following pair of code samples, which demonstrate a simple implementation for the aforementioned Tidy functions. Here's how the examples look:

    // example of 'tidy_parse_file()' function

    // definition of (target_file.htm)
    <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>

    $tidy=tidy_parse_file('target_file.html');
    $tidy->cleanRepair();
    if(!empty($tidy->errorBuffer)){
        trigger_error('Some errors occurred when parsing target
    file'.$tidy->errorBuffer,E_USER_ERROR);
    }

    // example of 'tidy_repair_file()' function
    $brokenFile='target_file.htm';
    $fixedFile=tidy_repair_file($brokenFile);
    if(!file_put_contents($brokenFile,$fixedFile)){
                trigger_error('Error putting fixed contents on target
    file',E_USER_ERROR);
    }

    As demonstrated above, the first example shows how to take advantage of the capacity provided by the "tidy_parse_file()" function to interpret the markup of a concrete (X)HTML file, which is finally corrected via the already familiar "cleanRepair()" method that you learned in a previous section.

    The second case illustrates a simple implementation of the "tidy_repair_file()" function, which comes in very convenient for reading and fixing the contents of a concrete (X)HTML file in only one step.

    At this stage, and after analyzing in detail the pair of hands-on examples listed a few lines above, hopefully you'll have a much better idea of how to use (at least basically) some of the most useful  functions that come integrated with the Tidy extension in PHP 5.

    Of course, this tutorial is intended to be a simple introduction to the main features provided by this excellent PHP 5 package. If you're searching for a more detailed reference on Tidy's functions and properties, the PHP official site is the best place to look.

    Final thoughts

    Unfortunately, we've come to the end of this first article of the series. As you saw in this tutorial, I walked you though the basic concepts of using the Tidy extension in conjunction with PHP 5. Nevertheless, this instructive journey is just beginning, since there are many other useful Tidy functions that still need to be properly reviewed, so you can acquire a more solid background in this helpful PHP extension.

    In the next installment of the series I'm going to teach you how to use some additional functions included with the Tidy library for extracting specific nodes of a given (X)HTML document.

    Now that you know what to expect from the next part, you won't want to miss it! 



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek