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 /* displays the following: // example on using the 'tidy_error_count()' function /* displays the following: // example on using the 'tidy_warning_count()' function $html='<p>This is an erroneous line</i>'; /* displays the following: 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>'; /* displays the following: echo 'Status of tidy object is the following'.tidy_get_status($tidyObj2); /* displays the following: 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|