<?
/*
clsTags.php parst ein HTML-Template und ersetzt vordefinierte Platzhalter
Copyright (C) 2004 Thomas Meinusch
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc.,\ 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
*/
class Tag{
var $CONTENT = "";
var $NAME = "";
var $Text = "";
var $Header = "";
var $Footer = "";
var $TagPara = array();
#************************************************************
// Konstruktor
// Tag($area);
// Tag($TagName, $area);
// Tag($TagName, $area, $outer);
// Tag($TagName, $area, $name, $outer);
function Tag($TagName = "", $area = "", $name="", $outer=true){
$TagName=trim($TagName);
$area=trim($area);
if($TagName=="" && $area=="") return $this=NULL;
// Tag finden
if($area==""){
$area=$TagName;
$TagName="";
}
if($TagName==""){
preg_match("/(<(\w+)(\ .*)*\/?>)/i", $area, $matches);
$TagName=$matches[2];
}
$this->NAME=$TagName;
if($TagName==""){
// echo substr($area,0,30);
// print_r($matches);
}
if(is_bool($name)){
$outer=$name;
$name="";
}
// $p = xml_parser_create();
// xml_parse_into_struct($p, $area, $vals, $index);
// xml_parser_free($p);
// if(!is_array($index[strtoupper($TagName)])){
// echo strtoupper($TagName)."";
// print_r($index[strtoupper($TagName)]);
// echo "<br>";
// return $this=NULL;
// }
// print_r($index[strtoupper($TagName)]);
// print_r($vals[strtoupper($TagName)]);
// print_r($index);
// print_r($vals);
// print_r($index);
if($this->getTag($area, $name, $outer)){
$this->getPara();
}else{
// unset($this);
return $this=NULL;
}
/*
echo "<textarea cols=50 rows=10>";
echo "H: ".$this->Header."\n";
echo "F: ".$this->Footer."\n";
echo "T: ".$this->Text."\n";
echo "C: ".$this->CONTENT."\n";
echo "P: ";
echo "</textarea>";
/*
*/
}
// Schneide Tag aus einem Bereich
function getTag($area, $name, $outer=true){
//Tagpositionen merken
$TagName=$this->NAME;
// <Tag name="$name"
if($name){
preg_match("/(<($TagName\ [^>]*)(name=\"($name)\")[^>]*\/?>)/i", $area, $matches);
if(strlen($matches[0])){
return $this->getTag(substr($area,strpos($area,$matches[0])), "$TagName");
}else{
// return ""; // keine weiteren vorkommen
}
}
// <Tag parameter nicht bekannt!!!
$posn=strpos($area, "<$TagName>");
$posp=strpos($area, "<$TagName ");
$pose=strpos($area, "</$TagName>");
// Start Tag
if(substr($area,min($posn, $posp),strlen($TagName)+1)=="<$TagName"){
$posn=min($posn, $posp);
}else{
$posn=max($posn, $posp);
}
$pose=strpos(substr($area, $posn+1), "</$TagName>");
// Inlinetag
if ($pose===false){
# if (!$posn) return false; // nichts zu tun!!!
if (substr($area, $posn,strlen($TagName)+1)!="<$TagName"){
return false; // nichts zu tun!!!
}
// <Tag( xyz="yyy")*
preg_match ("/(<($TagName)(\ \w+(-\w+)*=\"[^\">]*\")*[^>]*(\s*\/?)?>)/i", $area, $matches);
// if(strpos($matches[0],"case")) print_r($matches);
$this->CONTENT = $matches[0];
$this->Header = $matches[0];
return true;
}
$pose=$posn+$pose+1;
// Blocktag
$x=0;
$anz=10;
do{
// Bereich merken
if($x>0){
if($outer){
$pose+=$x;
}else{
$posn+=$x;
}
}
$temp=substr($area, $posn, $pose-$posn+strlen($TagName)+3);
// echo "<textarea cols=60 rows=5>".$temp."</textarea>\n";
// preg_match_all ("/(<($TagName)(\ .*\w+(-\w+)*=\"([^\"]*)\")*[^>]*>)/i", $temp, $mHeader);
preg_match_all ("/<($TagName)(\ \w+(-\w+)*=\"[^\"]*\"[^>]*)*>/i", $temp, $mHeader);
preg_match_all ("/(<\/($TagName)>)/i", $temp, $mFooter);
$x=0;
// Soll Bereich erweitert oder eingeschränkt werden?
if($outer){
$x=strpos(substr($area,$pose+strlen($TagName)+3), "</$TagName>")+strlen($TagName)+3;
# if(strpos(substr($area,$pose+strlen($TagName)+3)+$x, "</$TagName>")===false) $x=0;
// if(count($mHeader[0])>count($mFooter)){
// }
// if($TagName=="Adresse"){
// print_r($mHeader);
// print_r($mFooter);
// echo "-- $x --";
// echo "<textarea cols=60 rows=5>".substr($area,$pose+strlen($TagName)+3+10)."</textarea>";
// }
}else{
// if(count($mHeader[0])>0){
// $x=strpos($temp, $mHeader[0][1]);
// $x=strpos(substr($temp,3), $mHeader[0][1]);
while($x=strpos(substr($temp,3), "<$TagName")){
$posn+=$x+3;
$temp=substr($area, $posn, $pose-$posn+strlen($TagName)+3);
preg_match_all ("/(<($TagName)(\ .*\w+(-\w+)*=\"([^\"]*)\")*[^>]*>)/i", $temp, $mHeader);
}
// }
}
if($TagName=="sqlarea"){
// echo "<textarea>$temp</textarea> - TM: ".count($mHeader[0])." = ".count($mFooter[0])." ($x) <br>";
// echo "<textarea>".substr($area, $posn, $pose-$posn+strlen($TagName)+3)."</textarea> - TM: ".count($mHeader[0])." = ".count($mFooter)."<br>";
// echo "<textarea>".substr($area, $posn, $pose-$posn+strlen($TagName)+3)."</textarea> - TM: ".count($mHeader[0])." = ".count($mFooter)." ($x) <br>";
}
}while($x && $anz-- && count($mHeader[0]) && count($mFooter[0]) && count($mHeader[0])!=count($mFooter[0]));
if($TagName=="sql" || $TagName=="sqlarea"){
// echo "<textarea>".substr($area, $posn, $pose-$posn+strlen($TagName)+3)."</textarea> - TM: ".count($mHeader[0])." = ".count($mFooter)."<br>\n\n";
}
// Abbruch falls kein Header vorhanden!
if(count($mHeader[0])==0) return false;
$this->CONTENT = $temp;
$this->Header = $mHeader[0][0];
$this->Footer = $mFooter[0][count($mFooter[0])-1];
$this->Text = substr($this->CONTENT, strlen($this->Header),
strlen($this->CONTENT)-strlen($this->Header)-strlen($this->Footer));
// $this->Text = trim($this->Text);
// echo "<textarea cols=60 rows=5>".$this->Header."</textarea>";
return true;
}
function getPara(){
$para=substr($this->Header,strlen($this->NAME)+2);
// echo $this->Header."\n";
$para=substr($para, 0, strlen($para)-1);
// echo $this->NAME.":\n";
// wort-wort-...-wort=""
//new// preg_match_all ("/(\w+(-\w+)*)=([^\"\s]+|\"[^\"]*\")/", $para, $lines);
// preg_match_all ("/(\w+(-\w+)*)=\"([^(\"<>)]*|(<.*>[^\"])*)\"/", $para, $lines);
preg_match_all ("/(\w+(-\w+)?)=([^\"\s]+|\"[^\"]*\")/", $para, $lines);
for($i=0;$i<sizeof($lines[0]);$i++){
$keys=preg_split("/=/", $lines[0][$i],2,PREG_SPLIT_NO_EMPTY);
if (substr($keys[1],0,1)=="\""){
$keys[1]=substr($keys[1],1,strlen($keys[1])-2);
}
// echo " ".$keys[0]." = ".$keys[1]."\n";
$this->TagPara[$keys[0]]=$keys[1];
}
// echo "\n\n";
// Alle Blocktags, Text trimen?
switch($this->TagPara['trim']){
case "left" :
$this->Text = ltrim($this->Text);
break;
case "right" :
$this->Text = rtrim($this->Text);
break;
case "both" :
$this->Text = trim($this->Text);
break;
}
}
}
?>