PHP
  Home arrow PHP arrow Page 3 - Working with the Tidy Library in PHP 5
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working with the Tidy Library in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-06-26

    Table of Contents:
  • Working with the Tidy Library in PHP 5
  • Parsing (X)HTML strings
  • Implementing the tidy_clean_repair() function
  • Using the tidy_parse_file() and tidy_repair_file() functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with the Tidy Library in PHP 5 - Implementing the tidy_clean_repair() function


    (Page 3 of 4 )

    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


       · In case you didn't know, it's possible to access the main fatures of the popular...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT