HomePHP Page 2 - Tracking Parsing Errors with the Tidy Library in PHP 5
Summarizing some Tidy library concepts - 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.
Before I move on, I'd first like to remind you of the most important topics treated in the preceding article of the series. This review should help you see more easily how the Tidy functions learned in that tutorial can be linked with the ones that I plan to cover in a few moments.
Having said that, below I listed some basic examples of how to use the hopefully familiar "tidy_get_html()," "tidy_get_head()" and "tidy_get_body()" functions, which are tasked with extracting the different sections of a specified (X)HTML string. Here are the corresponding code samples; have a look at them please:
// example on using the 'tidy_get_html()' function
$html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Testing Tidy</title></head><body><p>Testing Tidy</p></body></html>'; $tidy=tidy_parse_string($html); $htmlNode=tidy_get_html($tidy); echo $htmlNode->value;
/* displays the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Testing Tidy</title> </head> <body><p>Testing Tidy</p> </body> </html> */
// example on using the 'tidy_get_head()' function
$html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Testing Tidy</title></head><body><p>Testing Tidy</p></body></html>'; $tidy=tidy_parse_string($html); $headNode=tidy_get_html($tidy); echo $headNode->value;
/* displays the following: <head> <title>Testing Tidy</title> </head> */
// example on using the 'tidy_get_body()' function
$html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Testing Tidy</title></head><body><p>Testing Tidy</p></body></html>'; $tidy=tidy_parse_string($html); $bodyNode=tidy_get_body($tidy); echo $bodyNode->value;
/* displays the following: <body> <p>Testing Tidy</p> </body> */
Hopefully, after studying the respective signatures for the three previous examples, you'll recall how easy it is to extract the distinct sections of a specified (X)HTML string using the functions provided by the Tidy library. Nonetheless, I believe that these functions in particular don't bear any additional discussion, since they were already covered in detail in the previous tutorial of the series.
Therefore, it's a good time to move forward and start looking into the group of Tidy functions aimed specifically at handling all the errors that occur when parsing a concrete (X)HTML string.
Want to see how these brand new functions will be properly implemented? Click on the link below and keep reading.