HomePHP Page 3 - Tracking Parsing Errors with the Tidy Library in PHP 5
Using the tidy_get_error_buffer() function - 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.
As I stated in the section that you just read, not surprisingly the Tidy library comes equipped with a neat group of functions whose primary capacity is to return to calling code all the errors raised when parsing a specified (X)HTML string.
Let me show you the first function that belongs to this specific group. It's called "tidy_get:error_buffer()," and it allows you to easily retrieve, from Tidy's error buffer, all the warnings raised when interpreting a concrete (X)HTML string. The corresponding code sample is as follows:
// example on using the ' tidy_get_error_buffer()' function $html='<p>This paragraph will be parsed by tidy</p>'; $tidy=tidy_parse_string($html); echo tidy_get_error_buffer($tidy);
/* displays the following: line 1 column 1 - Warning: missing <!DOCTYPE> declaration line 1 column 1 - Warning: inserting missing 'title' element */
As illustrated above, the "tidy_get_error_buffer()" function displays all the errors that occurred while parsing a concrete (X)HTML string. In this case, I coded some badly-formatted (X)HTML data, and then used the function to show the pertinent warnings thrown by "tidy_parse_string()." Not too complex, right?
However, as I said earlier, the Tidy library comes with a remarkable set of functions for retrieving all of the potential errors triggered when parsing some (X)HTML data. Therefore, in the following section I'm going to teach you how to use these additional error-handling functions. Click on the link that appears below and keep reading.