PHP4 use __construct() */ function SinglePage($tempname, $filename){ $this->tempname = $tempname; $this->filename = $filename; $this->file = $tempname."/".$filename; /* * Check to see if some very important files exists */ if(!is_dir($tempname)){ die("There is no template with that name."); }else if(!file_exists($this->file)){ die("There is no file named ".$this->file); } } /* * Build the entire contents, and once we have it, we can do everything else we want to it later! */ function buildContents(){ $content = NULL; if(!file_exists($this->file)){ die("The file you are trying to use as a template doesnt exists (error: 001)"); } // Catch the file $tr = @fopen($this->file, 'r'); if($tr){ $content = fread($tr, filesize($this->file)); } // NOW we're rollin', let's release this file too. unset($tr); $this->content = $content; } /* * Get all the replacements in an array */ function replace($replace, $with){ if(!is_array($with)){ $i = count($this->doReplace); $j = count($this->replaceWith); $this->doReplace[$i] = $replace; $this->replaceWith[$j] = $with; }else{ $this->replaceArray($replace, $with); } } function replaceArray($replace, $with){ $j = count($this->doReplaceArray); $n = count($this->replaceWithArray); if($n!=$j){ if($n>$j){ $ar = $n; }else{ $ar = $j; } }else{ $ar = $j; } $withi = count($with); for($i=0;$i<=count($withi);$i++){ $this->doReplaceArray[$ar][$i] = $replace; $this->replaceWithArray[$ar][$i] = $with[$i]; } } function doReplacements(){ for($i=0;$idoReplace);$i++){ while(preg_match_all('/%'.$this->doReplace[$i].'%/i', $this->content, $matches)>0){ $this->content = preg_replace('/%'.$this->doReplace[$i].'%/i', $this->replaceWith[$i], $this->content); } } } function doInclude(){ while(preg_match_all("/%include=((\.+)?(\/?\w+\/)?(\/)?(\w+\.php))/i", $this->content, $matches)>0){ $includes = preg_match("/%include=((\.+)?(\/?\w+\/)?(\/)?(\w+\.php))/i", $this->content , $matches); $this->content = preg_replace("/%include=((\.+)?(\/?\w+\/)?(\/)?(\w+\.php))/i", '', $this->content); include($matches[1]); } } function parseTemplate(){ $this->doReplacements(); $this->doInclude(); return $this->content; } } $temp = new SinglePage("main", "this.html"); $temp->buildContents(); $temp->replace('date', array(date(r), date('D M d Y'))); $temp->replace('hi', 'Hello'); echo $temp->parseTemplate(); ?>