home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / HTMLtoXHTML.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  3.3 KB  |  117 lines

  1. <?php
  2. /**
  3.  * $Id: HTMLtoXHTML.php,v 1.1 2003/08/11 13:02:50 harryf Exp $
  4.  * Demonstrates conversion of HTML to XHTML
  5.  */
  6. require_once('XML/XML_HTMLSax.php');
  7.  
  8. class HTMLtoXHTMLHandler {
  9.     var $xhtml;
  10.     var $inTitle;
  11.     var $pCounter;
  12.  
  13.     function HTMLtoXHTMLHandler(){
  14.         $this->xhtml='';
  15.         $this->inTitle = false;
  16.         $this->pCounter=0;
  17.     }
  18.  
  19.     // Handles the writing of attributes - called from $this->openHandler()
  20.     function writeAttrs ($attrs) {
  21.         if ( is_array($attrs) ) {
  22.             foreach ( $attrs as $name => $value ) {
  23.  
  24.                 // Watch for 'checked'
  25.                 if ( $name == 'checked' ) {
  26.                     $this->xhtml.=' checked="checked"';
  27.                 // Watch for 'selected'
  28.                 } else if ( $name == 'selected' ) {
  29.                     $this->xhtml.=' selected="selected"';
  30.                 } else {
  31.                     $this->xhtml.=' '.$name.'="'.$value.'"';
  32.                 }
  33.             }
  34.         }
  35.     }
  36.  
  37.     // Opening tag handler
  38.     function openHandler(& $parser,$name,$attrs) {
  39.         if ( (isset ( $attrs['id'] ) && $attrs['id'] == 'title') || $name == 'title' )
  40.             $this->inTitle=true;
  41.  
  42.         switch ( $name ) {
  43.             case 'br':
  44.                 $this->xhtml.="<br />\n";
  45.                 break;
  46.             case 'html':
  47.                 $this->xhtml.="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"eng\">\n";
  48.                 break;
  49.             case 'p':
  50.                 if ( $this->pCounter != 0 ) {
  51.                     $this->xhtml.="</p>\n";
  52.                 }
  53.                 $this->xhtml.="<p>";
  54.                 $this->pCounter++;
  55.                 break;
  56.             default:
  57.                 $this->xhtml.="<".$name;
  58.                 $this->writeAttrs($attrs);
  59.                 $this->xhtml.=">\n";
  60.                 break;
  61.         }
  62.  
  63.     }
  64.  
  65.     // Closing tag handler
  66.     function closeHandler(& $parser,$name) {
  67.         if ( $this->inTitle ) {
  68.             $this->inTitle=false;
  69.         }
  70.         if ($name == 'body' && $this->pCounter != 0)
  71.             $this->xhtml.="</p>\n";
  72.  
  73.         $this->xhtml.="</".$name.">\n";
  74.     }
  75.  
  76.     // Character data handler
  77.     function dataHandler(& $parser,$data) {
  78.         if ( $this->inTitle )
  79.             $this->xhtml.='This is XHTML 1.0';
  80.         else
  81.             $this->xhtml.=$data;
  82.     }
  83.  
  84.     // Escape handler
  85.     function escapeHandler(& $parser,$data) {
  86.         if ( $data == 'doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"' )
  87.             $this->xhtml.='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  88.     }
  89.  
  90.     // Return the XHTML document
  91.     function getXHTML () {
  92.         return $this->xhtml;
  93.     }
  94. }
  95.  
  96. // Get the HTML file
  97. $doc=file_get_contents('example.html');
  98.  
  99. // Instantiate the handler
  100. $handler=& new HTMLtoXHTMLHandler();
  101.  
  102. // Instantiate the parser
  103. $parser=& new XML_HTMLSax();
  104.  
  105. // Register the handler with the parser
  106. $parser->set_object($handler);
  107.  
  108. // Set the handlers
  109. $parser->set_element_handler('openHandler','closeHandler');
  110. $parser->set_data_handler('dataHandler');
  111. $parser->set_escape_handler('escapeHandler');
  112.  
  113. // Parse the document
  114. $parser->parse($doc);
  115.  
  116. echo ( $handler->getXHTML() );
  117. ?>