検索
ホームページphp教程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);



本文地址:

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

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。