Home arrow PHP arrow Page 4 - Tracking Parsing Errors with the Tidy Library in PHP 5

Using the tidy_access_count(), tidy_error_count() and tidy_warning_count() functions - PHP

Creating well-formed (X)HTML documents can be a hard-to-accomplish task, particularly for PHP developers who need to focus mainly on the data and business layers of their web applications, and not on their visual presentation modules. However, this issue can be addressed with minor hassles with the assistance of the excellent Tidy extension.

TABLE OF CONTENTS:
  1. Tracking Parsing Errors with the Tidy Library in PHP 5
  2. Summarizing some Tidy library concepts
  3. Using the tidy_get_error_buffer() function
  4. Using the tidy_access_count(), tidy_error_count() and tidy_warning_count() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 11, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: