<?php
class template{
var $temp;               //读取的模板页文件名(包含路径)
var $html;               //要生成的html文件名(包含路径)
var $err;                //错误编号
var $test;               //数据流
var $arr;                //要替换的数组 模式(键名->模板标签 , 键值->替换内容)
function template(){
$this->temp="";               
$this->html="";               
$this->err=0;                
$this->test=""; 
}
function templatehtml($temp,$html,$arr){
$err=$this->chkfile($temp);
if((int)$err==0){ 
$fp=fopen($temp,"r");                      //只读方式打开模板页
$test=fread($fp,filesize($temp));          //读取模板页的数据流 
$test=$this->arr_replace($arr,$test);      //替换文件     
$err=$this->writefile($html,$test);        //生成静态页
}
//echo "由模板页 ".$temp." 生成 ".$html.$this->error($err);
return;
}    /*
*判断文件是否存在
*返回错误提示
*/ function chkfile($file){
if(file_exists($file)){
return 0;
}
return 1;

   
/*
*根据数组文件内容,替换数据流  模式(键名->模板标签 , 键值->替换内容)
*返回数据流
*参数$arr:数组
*参数$test :数据流
*/  
function arr_replace($arr,$test){
$ss=$test;
foreach ($arr as $key => $value){
$ss= str_replace($key,$value,$ss);
}
return $ss;
}
     
/*
*将数据流,写入到文件中
*返回执行状态
*参数$html:要生成的html文件
*参数$test :数据流
*/     
function writefile($html,$test){
$stat=2;
if($this->chkfile($html)==0){  //判断文件是否存在    
  $stat=0;                     //已经存在返回0
}
if($f=fopen($html,"w")){       //写入方式打开文件,不存在则创建    
fputs($f,$test);
fclose($f);
$stat=0;                   //写入成功返回0
}else{
$stat=2;                   //写入失败返回2
}
return $stat;

/*
*错误提示
*返回错误提示
*参数$err:错误编号
*参数$file :错误文件
*/    
function error($err){
$message=""; 
switch((int)$err){
case 0 : 
$message=" 静态页生成成功";
break;
case 1 :
$message=" 模板页打开失败,请检查是否存在";
break;
case 2 :
$message=" 文件生成失败,请检查目录权限";
break;
default:
$message=" 未知错误";
}
return $message;
}
 
/*
*
*主要用来读取模板页,返回数据流  (比如top,foot公用文件,)
*参数$file :模板页路径
*/ 
 
function readhtml($file){
$test="";
$err=$this->chkfile($file);
if($err==0){
$fp=fopen($file,"r");                       //只读方式打开模板页
$test=fread($fp,filesize($file));          //读取模板页的数据流 
}else{
$test=$file.$this->error($err);
}    
return $test;
}/*
*
*主要用来删除已生成的文件,不返回
*参数$file :文件路径
*/ function delete_file($file){ 
if(file_exists($file)){ 
$delete = chmod ($file, 0777); 
$delete = unlink($file); 
if(file_exists($file)){ 
$filesys = eregi_replace("/","",$file); 
             $delete = system("del $filesys"); 
             clearstatcache(); 
            if(file_exists($file)){ 
                 $delete = chmod ($file, 0777); 
                 $delete = unlink($file); 
                 $delete = system("del $filesys"); 
             } 
         } 
clearstatcache(); 

}   
}
?>

解决方案 »

  1.   

    请给出示例代码!测试了一下,没有发现你说的现象$s =<<< TXT
    "{a}" '{a}' {a}
    TXT;
    file_put_contents('zz', $s);
    $ar = array('{a}' => 'xxx');
    $p = new template;
    $p->templatehtml('zz', 'zz.htm', $ar);
    readfile('zz.htm');
    "xxx" 'xxx' xxx
      

  2.   

    执行php文件 $content="<div class="header">
    <div class="head">
    <div class="logo"></div>
    <div class="nav">
    <ul>
    <li>$label[111]</li>
    <li>$label[222]</li>
    <li>$label[333]</li>
    </ul>
    </div>
    </div>
    </div>"
            require_once(dirname(__FILE__)."/demo.php");
    $sc=new template();
    $tmp="mo.html";        //读取母模板
    $filename=test.html";   //生成新模板
    $arr=array();
    $arr["{title}"]="$title";
    $arr["{text}"]="$content"; 
    $sc->templatehtml($tmp,$filename,$arr);mo.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>{title}</title>
    </head><body>
    {text}
    </body>
    </html>
      

  3.   

    $content="<div class="header">
    <div class="head">
    ....形如这样的语句是不可能通过语法分析的,当然示例一下也不关紧要
    是你取到的 $content 中的引号已经被转义了
    $s =<<< TXT
    "{a}" '{a}' {a}
    TXT;
    file_put_contents('zz', $s);
    $ar = array('{a}' => '"aaa"'); //注意这里
    $p = new template;
    $p->templatehtml('zz', 'zz.htm', $ar);
    readfile('zz.htm');
    "aaa"""aaa"" '"aaa"' "aaa"