PHP
  Home arrow PHP arrow Page 4 - Tracking Parsing Errors 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

Tracking Parsing Errors with the Tidy Library in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-07-11


    Table of Contents:
  • Tracking Parsing Errors with the Tidy Library in PHP 5
  • Summarizing some Tidy library concepts
  • Using the tidy_get_error_buffer() function
  • Using the tidy_access_count(), tidy_error_count() and tidy_warning_count() 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


    Tracking Parsing Errors with the Tidy Library in PHP 5 - Using the tidy_access_count(), tidy_error_count() and tidy_warning_count() functions
    ( Page 4 of 4 )

    Counting the errors and warnings raised when parsing a specified (X)HTML string can be a no-brainer process with the assistance of some handy functions included with the Tidy extension.

    In this case, I'm talking about the useful "tidy_access_count()," "tidy_error_count()" and "tidy_warning_count()" functions. As their names clearly suggest, they are tasked with keeping track of any potential failure triggered at the time of interpreting and fixing a given (X)HTML string.

    All right, having introduced these brand new error-handling functions bundled with the Tidy extension, I'm going to show you some hands-on examples which demonstrate in a friendly fashion how to use the functions in question in some concrete cases. Please take a look at them:

    // example on using the 'tidy_access_count()' function

    $html='<html><head><title>This file will be parsed by
    Tidy</title></head><body><p>This is paragraph</p></body></html>';
    // set accessibility check level: 1, 2 or 3
    $params=array('accessibility-check'=>3);
    $tidy=tidy_parse_string($html,$params);
    $tidy->CleanRepair();
    $tidy->diagnose();
    echo tidy_access_count($tidy);

    /* displays the following:
    5
    */

    // example on using the  'tidy_error_count()' function
    $html='<p>This is an erroneous line</i>';
    $tidy=tidy_parse_string($html);
    echo 'Number of errors encountered when parsing string is the following:'.tidy_error_count($tidy);

    /* displays the following:
    1
    */

    // example on using the 'tidy_warning_count()' function

    $html='<p>This is an erroneous line</i>';
    $tidy=tidy_parse_string($html);
    echo 'Number of errors encountered when parsing string is the following:'.tidy_warning_count($tidy);

    /* displays the following:
    Number of errors encountered when parsing string is the following:
    4
    */

    As illustrated above, the first example uses the "tidy_access_count()" function to display the number of errors triggered when parsing a sample (X)HTML string. Also, it's worth noting here that this function is used along with another one named "diagnose()." Unfortunately, the reason for engaging in this coupling process hasn't been specified yet in the official PHP documentation, so for now you'll have to take this example as it is.

    Now that I have clarified the issue surrounding the implementation of the "tidy_access_count()" function, I will explain the second hands-on example. In this case, the number of errors triggered at the time of parsing a sample (X)HTML string is displayed on the browser via the simple "tidy_error_count()" function, which certainly doesn't bear too much discussion here.

    And finally, the third example demonstrates how to count the number of warnings thrown when parsing the same sample (X)HTML string utilizing the "tidy_warning_count()" function.

    In addition to the error-handing functions discussed above, I'd like to show you one more, named "tidy_get_status()," which comes in handy for determining the status of a tidy object after parsing a couple of badly-formatted (X)HTML strings.

    The corresponding code sample is as follows:  

    // example on using the 'tidy_get_status()' function

    $badhtml1='<p>This is an erroneous line</i>';
    $tidyObj1=tidy_parse_string($html);
    $badhtml2='<p>This is another erroneous line</i>';
    $tidyObj2=tidy_parse_string($html2);
    echo 'Status of tidy object is the following: '.tidy_get_status
    ($tidyObj1);

    /* displays the following:
    Status of tidy object is the following: 1
    */

    echo 'Status of tidy object is the following'.tidy_get_status($tidyObj2);

    /* displays the following:
    Status of tidy object is the following: 2
    */

    As you can see, the above hands-on example simply shows how a tidy object, which is returned by the already familiar "tidy_parse_string()" function, can modify its status in consonance with the errors raised when interpreting a specified (X)HTML string via the "tidy_get_status()" function.

    Even though it's clear to see that the prior function has a rather limited utility in certain cases, it deserves at least a basic analysis to complete the coverage of error-handing functions included with the Tidy extension.

    Final thoughts

    That's all for the moment. Sadly, we've come to the end of this series, but hopefully the experience has been pretty instructive. As you learned in these three tutorials, the Tidy library can be really useful if you're a PHP developer who doesn't spend much time formatting the (X)HTML documents included in your web applications.

    See you in the next PHP tutorial!



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