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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 - 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! 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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