Login   Register  
PHP Classes
elePHPant
Icontem

File: example4.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Alexey G. Piyanin  >  HTML SAX Parser  >  example4.php  >  Download  
File: example4.php
Role: Example script
Content type: text/plain
Description: Example #4 (get body tag content)
Class: HTML SAX Parser
Parse HTML documents using regular expressions
Author: By
Last change:
Date: 9 years ago
Size: 935 bytes
 

Contents

Class file image Download
<?
/*
Author: Alexey G. Piyanin (e-mail: drdrzlo at mail dot ru)
Date:   Mar 28 2005
Title:  Get body tag content
*/
include('SAXParser.php');

$beginPos = -1;
$endPos = -1;

function 
begin($tag,$attributes,$pos,$length){
  global 
$beginPos;
  if (
$tag=='body' && $beginPos<0$beginPos $pos+$length;
}

function 
endTag($tag,$pos){
  global 
$endPos;
  if (
$tag=='body' && $endPos<0$endPos $pos;
}

$parser = new HTML_SAXParser();
$parser->initFunc('begin','endTag');?>
<html>
<body>
<center>Source page:<br><iframe src="example1.html" width="600" height="400" ></iframe></center><br><br>
Body content:<br><pre><?
$contentHTML 
join('',file('example1.html'));
$parser->parseString($contentHTML);
if (
$beginPos>&& $endPos>0){ // find body tag
  
$contentHTML substr($contentHTML,$beginPos,$endPos-$beginPos); // cut body content
  
echo htmlspecialchars($contentHTML);
}
?>
</pre></body></html>