PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<HTML>
<HEAD>
<TITLE>SMTemplates V1.00</TITLE>
<STYLE>
td,body{font-family:tahoma; font-size: 11px; color:black}
</STYLE>
</HEAD>
<BODY>
<b>This file demonstrates the use of SMTemplates.</b><br><br>
This is header.tpl<br>
{{ID1}}<br>
{{ID2}}<br>
<!-- BEGIN SUBBLOCK -->
<!-- BEGIN SUBBLOCK1 -->
IM IN SUBBLOCK1
<!-- END SUBBLOCK1 -->
IM IN SUBBLOCK<br>
<!-- END SUBBLOCK -->
Is there any possible way to store each and every one of those blocks in an array?
i.e. given the template above, i should wind up with this array
Sure. If you read in the file into an array (with file("filename")) and then cycle through each element of the array you can construct the logic within the loop to fit each piece into the element of a new array that fits your requirements. XML would probably work a bit better as a source file for handling something like this, but well-structured HTML could work just as well.
$linecount = count($lines = explode("\n", $contents));
for($i = 0; $i <= $linecount; $i++ ) {
preg_match("<!-- BEGIN (.*) -->", $linesї$i], $m);
if( $m || ($OPEN && $OPEN != '$BLOCKS') ) {
// If we find a new <!-- BEGIN їNAME] -->, then:
if( $m && $OPEN ) {
// A BLOCK IS ALREADY OPENED & AN INNERBLOCK IS FOUND
// NOW APPEND TO $OPEN AND eval() IT.
$OPEN .= 'ї'.$mї1].']';
$BLOCKSїMAIN] .= "<TPL BLOCK="".$OPEN."">\n";
//eval("$OPEN .= \$linesї\$i];");
print "$OPEN = $linesї$i]<BR>\n";
} elseif( $m && !$OPEN ) {
// A PARENT BLOCK IS FOUND ($OPEN DOESN"T EXIST
// AND <!-- BEGIN їNAME] --> WAS FOUND).
// NOW CREATE $OPEN AND eval() it.
$OPEN = '$BLOCKSї'.$mї1].']';
$BLOCKSїMAIN] .= "<TPL BLOCK="".$OPEN."">\n";
//eval("$OPEN = \$linesї\$i];");
print "$OPEN = $linesї$i]<BR>\n";
} elseif( !$m && $OPEN ) {
// A BLOCK IS OPEN AND THERE IS NO NEW BLOCK FOUND,
// SO JUST APPEND $linesї$i] TO CURRENT WORKING BLOCK
//eval("$OPEN .= \$linesї\$i];");
print "$OPEN = $linesї$i]<BR>\n";
}
if( ( ($m && $OPEN) || ($m && !$OPEN) || (!$m && $OPEN) ) && preg_match("<!-- END (.*) -->", $linesї$i], $n) ) {
// WE FOUND A <!-- END їNAME] -->, SO CLOSE WORKING BLOCK
$OPEN = preg_replace("/(.+)(\ї.+\]){1}$/", "\\1", $OPEN);
}
} else {
// NO WORKING BLOCK, SO APPEND THE CURRENT LINE TO $BLOCKїMAIN]
$BLOCKSїMAIN] .= $linesї$i]."\n";
}
}