class create_html { private $template; //模版 private $file_name; //檔案名稱 private $array; //資料數組 function __construct($file_name, $template, $array) { //構造類 $this->template = $this->read_file($template, "r"); //讀模數板檔案 $this->file_name = $file_name; $this->array = $array; //資料資料 $this->html(); //產生html } function html() { //產生html while (ereg ("{([0-9]+)}", $this->template, $regs)) { //迴圈模版中所能的{1}….. $num = $regs[1]; //得到1、2、3序列 $this->template = ereg_replace("{".$num."}", $this->array[$num], $this->template); //把資料替換成html內容 $this->write_file($this->file_name, $this->template, "w+"); //產生HTML檔案 } } function read_file($file_url, $method = "r") { //讀取檔案 $fp = @fopen($file_url, $method); //開啟檔案 $file_data = fread($fp, filesize($file_url)); //讀取檔案資訊 return $file_data; } function write_file($file_url, $data, $method) { //寫入檔案 $fp = @fopen($file_url, $method); //開啟檔案 @flock($fp, LOCK_EX); //鎖定檔案 $file_data = fwrite($fp, $data); //寫入檔案 fclose($fp); //關閉檔案 return $file_data; } } #例子———————- #讀取郵件回複模版———————————————————————————- $title = "標題"; $navigation = "瀏覽器"; $happy_origin = "作者"; $name = "test2.htm"; $template = "default_tmp.php"; //模版中用{1}{2}來替換 $daytype = array(1 => $title, 2 => $navigation, 3 => $happy_origin); $htm = new Restore_email($template, $daytype); echo $htm->pint(); ?> |