XML_HTMLSax3
[ class tree: XML_HTMLSax3 ] [ index: XML_HTMLSax3 ] [ all elements ]

Source for file HTMLtoXHTML.php

Documentation is available at HTMLtoXHTML.php

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

Documentation generated on Mon, 11 Mar 2019 15:11:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.