search
Homephp教程php手册一个php生成html类程序

来这里直接用构造函数来实现的,考虑到会有很多的生成静态页的进程,还是改为函数调用,本处保留备用,在类中template()替换成templateHtml()即可实现,

*/

 代码如下 复制代码

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();
    }
}
  
}

/*
调用方法

demo.html的代码

{title}
{text}


$sc=new template();
//$sc=new template($tmp,$filename,$arr);   ()

$tmp="../template/temp.php教程";    //模板页
$filename="test.html";          //生成页
$foot="../foot.html";           //包含的底文件,头文件同理

$arr=array();
$arr["{title}"]="新标题";
$arr["{text}"]="新内容";
$arr["{foot}"]=$sc->readhtml($foot);

 

$sc->templatehtml($tmp,$filename,$arr);



本文地址:

转载随意,但请附上文章地址:-)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.