parser.py (I suggest to put this in a class so that you can call it easily.)
<?php
function LoadTemplate($file="")
{
$doc_root = getenv('DOCUMENT_ROOT');
$strfile = "$doc_root/".$file;
if(!file_exists($strfile)) print"Loading template failed!";
$thisfile = file($strfile);
while(list($line,$value) = each($thisfile)) {
$value = ereg_replace("(r|n)","",$value);
$result .= "$valuern";
}
return
$result;
}
function
ReplaceStatic($LoadTemplate="", $StaticName="", $StaticValue="")
{
$tcontent = $LoadTemplate;
$j = count($StaticName);
for($i=0;$i<$j;$i++) {
$tcontent = eregi_replace($StaticName[$i],$StaticValue[$i],$tcontent);
}
return
$tcontent;
}
function
DynamicRows($Prefix="", $LoadTemplate="", $RowName="", $RowValue="")
{
// start loopings
$tcontent = $LoadTemplate;
$startlist = strpos($tcontent,"<!--%".$Prefix."_BEGIN_MESSAGE_LIST%-->");
$endlist = strpos($tcontent,"<!--%".$Prefix."_END_MESSAGE_LIST%-->")+28;
$listline = substr($tcontent,$startlist+30,$endlist-$startlist-58);
$listlinetoreplace = substr($tcontent,$startlist,$endlist-$startlist);
$startlistloop = strpos($tcontent,"<!--%".$Prefix."_ML_LOOPBEGIN%-->");
$endlistloop = strpos($tcontent,"<!--%".$Prefix."_ML_LOOPEND%-->")+22;
$listloop = substr($tcontent,$startlistloop+24,$endlistloop-$startlistloop-46);
$beforeloop = substr($listline,0,strpos($listline,"<!--%".$Prefix."_ML_LOOPBEGIN%-->"));
$afterloop = substr($listline,strpos($listline,"<!--%".$Prefix."_ML_LOOPEND%-->")+22);
$newmsgs = 0;
for($i=0;$i<count($headers);$i++)
if(!$headers[$i]["read"]) $newmsgs++;
$msglist .= eregi_replace("<!--%POLERIODUMMY%-->","Polerio",$beforeloop);
$start_pos=0;$end_pos=(count($RowValue));
for($i=$start_pos;$i<$end_pos;$i++) {
$thisline = "$listlooprn";
$s_pos=0;$e_pos=count($RowName);
for($j=$s_pos;$j<$e_pos;$j++) {
$thisline = eregi_replace($RowName[$j],$RowValue[$i][$j],$thisline);
}
$msglist .= $thisline;
} // end for
$msglist .= $afterloop;
$tcontent = substr($tcontent,0,$startlist).$msglist.substr($tcontent,$endlist,strlen($tcontent));
// end loopings
return $tcontent;
}
function
EmptySpace($Prefix="", $LoadTemplate="")
{
$tcontent = $LoadTemplate;
$startnolist = strpos($tcontent,"<!--%".$Prefix."_BEGIN_NO_MESSAGES%-->");
$endnolist = strpos($tcontent,"<!--%".$Prefix."_END_NO_MESSAGES%-->")+27;
$nomessagesline =
substr($tcontent,$startnolist+29,$endnolist-$startnolist-56);
$nomessageslinetoreplace =
substr($tcontent,$startnolist,$endnolist-$startnolist);
$tcontent = eregi_replace($nomessageslinetoreplace,"",$tcontent);
return $tcontent;
}
$file="polerio/art/template.html";
$LoadTemplate = LoadTemplate($file);
$StaticName = array("<!--%ModuleTitle%-->", "<!--%SomeComment%-->", "<!--%FixedValues%-->");
$StaticValue = array("Polerio and PHPBUILDER", "Article about parsing", "Some Fixed Values");
$LoadTemplate = ReplaceStatic($LoadTemplate, $StaticName, $StaticValue);
$result[0] = array("09/28/02","Parsing made simple","Juan Tamad");
$result[1] = array("09/30/02","PHP template revisited","Polerio Babao");
//$result=""; // Uncomment this to get hide result table.
if(!$result) $LoadTemplate = EmptySpace("97", $LoadTemplate); // If this does not exist, table w/o value will exist
else {
$Prefix = "9a"; $RowValue = $result;
$RowName = array("<!--%9adate_____%-->","<!--%9atitle____%-->","<!--%9aauthor___%-->");
$LoadTemplate = DynamicRows($Prefix, $LoadTemplate, $RowName, $RowValue);
}
$content = $LoadTemplate;
echo $content;
?>