Home arrow PHP arrow Page 3 - Working with the Tidy Library in PHP 5

Implementing the tidy_clean_repair() function - PHP

As a PHP developer, you've probably developed database-driven applications that deliver their contents in (X)HTML format to the end user. If so, you know that when you work directly with hard-coded (X)HTML files, you risk forgetting to close tags and DTD headers, making the process annoying and time-consuming. Keep reading; help is on the way.

TABLE OF CONTENTS:
  1. Working with the Tidy Library in PHP 5
  2. Parsing (X)HTML strings
  3. Implementing the tidy_clean_repair() function
  4. Using the tidy_parse_file() and tidy_repair_file() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 26, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated previously, the Tidy library comes equipped with another useful function, named "tidy_clean_repair()," which behaves in a manner nearly identical to the "cleanRepair()" method demonstrated in the section that you just read. In this case, this brand new function will fix any badly-formatted (X)HTML markup, and its usage is illustrated by the following example:

// example of 'tidy_clean_repair()' function
$html='<html><head><title>This file will be parsed by
Tidy</title></head><body><p>This is an erroneous line</i>This is
another erroneous line</i></body></html>';
$tidy=tidy_parse_string($html);
tidy_clean_repair($tidy);
echo $tidy;

/* 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>
      This file will be parsed by Tidy
    </title>
  </head>
  <body>
    <p>This is an erroneous line</p>
    <p>This is another erroneous line</p>
  </body>
</html>

As you can see, using the above "tidy_clean_repair()" function is indeed a very straightforward process, since the function in question performs a clean-up task on a specified (X)HTML string, certainly behaving identically to its cousin "cleanRepair()" method.

Additionally, when it comes to correcting the format of a specific (X)HTML string, the Tidy library also offers the neat "tidy_repair_string()" function, which can be used as indicated below:

// example of 'tidy_repair_string()' function
ob_start();
?>
<html>
  <head>
   <title>This file will be parsed by Tidy</title>
  </head>
  <body>
   <p>This is an erroneous line
   <p>This is another erroneous line</i>
  </body>
</html>
<?php
$fileContents=ob_get_clean();
$tidy=tidy_repair_string($fileContents);
echo $tidy;

/* 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>
      This file will be parsed by Tidy
    </title>
  </head>
  <body>
    <p>This is an erroneous line</p>
    <p>This is another erroneous line</p>
  </body>
</html>
*/

So far, so good, right?. At this point you've hopefully learned how to use a few useful functions included with the Tidy library to format correctly a particular (X)HTML string. Nevertheless, as you might have guessed, Tidy has plenty of neat functions when it comes to fixing badly-formatted markup.

Thus, keeping in mind this important fact, in the section to come I'm going to show you how to use the excellent functionality provided by the Tidy extension to parse, and eventually correct, the format of different (X)HTML files.

As you know, this brand new Tidy feature will be covered in the next few lines, so click on the link that appears below and keep reading.



 
 
>>> 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: