PHP Classes

File: example2.php

Recommend this page to a friend!
  Classes of Alexey G. Piyanin   HTML SAX Parser   example2.php   Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: Example #2 (page tree)
Class: HTML SAX Parser
Parse HTML documents using regular expressions
Author: By
Last change: Fix tag without end tag process
Date: 19 years ago
Size: 1,558 bytes
 

Contents

Class file image Download
<?
/*
Author: Alexey G. Piyanin (e-mail: drdrzlo at mail dot ru)
Date: Feb 15 2005
Title: Page tree
*/
include('SAXParser.php');

function
begin($tag,$attributes,$readSize){
  global
$stack,$t;?>
<table cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td><?=str_repeat('&nbsp;',5*sizeof($stack))?></td>
        <td><nobr><b><?=$tag?></b>: <?var_dump($attributes)?></nobr></td>
      </tr>
    </table>
<?if (!in_array($tag,$t)) array_unshift($stack,$tag);
}

function
endTag($tag,$readSize){
  global
$stack;
  while(
reset($stack)!=$tag && !empty($stack)) array_shift($stack);
 
array_shift($stack);?>
<table cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td><?=str_repeat('&nbsp;',5*sizeof($stack))?></td>
        <td><nobr><b>/<?=$tag?></b></td>
      </tr>
    </table>
<?}

function
character($str){
  global
$stack;?>
<table cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td><?=str_repeat('&nbsp;',5*sizeof($stack))?></td>
        <td style="border: 1px solid #000000;"><?=htmlspecialchars($str)?><?if (trim($str)==''){?>&nbsp;<?}?></td>
      </tr>
    </table>
<?
}

$t = array('br','meta','img','spacer','input','base','hr','link',);
$stack = array();
$URL = 'http://yahoo.com';

$parser = new HTML_SAXParser();
$parser->initFunc('begin','endTag','character');?>
<html>
<body>
<center>Source page:<br><iframe src="<?=$URL?>" width="600" height="400" ></iframe><br><br>
Page tree:<br></center>
<?$parser->parse($URL);?>
</body></html>