加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php静态化类

发布时间:2020-12-13 21:22:45 所属栏目:PHP教程 来源:网络整理
导读:一、静态化类 复制以下代码,存为文件static.class.php 1 ? php 2 /* * 3 * filename: static.class.php 4 * @author: phpwk http://www.55nav.com 5 * @copyright: Copyright 2011 phpwk 6 * @license: version 1.0 7 * @create: 2011-11-22 8 * @modify: p

一、静态化类
复制以下代码,存为文件static.class.php

 1 <?php
 2 /**
 3 * filename:      static.class.php
 4 * @author:       phpwk http://www.55nav.com
 5 * @copyright:    Copyright 2011 phpwk
 6 * @license:      version 1.0
 7 * @create:       2011-11-22
 8 * @modify:       phpwk 2011-11-23
 9 * description:   静态类,可以将php输出的内容生成静态文件,还有替换指定标签功能
10 * method of use: 至少传递三个参数$buffer,$filename,$filepath
11 * example:
12 */
13 class Shtml
14 {
15     function Shtml()
16     {
17         $this->Templet      = "";
18         $this->DataSource   = array();    
19         $this->fileName     = ""20         $this->mod          = "wb"21         $this->handle       = false22     }
23     //绑定数据源,参数为一数组
24     function BindData($arr)
25 26         $this->DataSource = 27 28     设置路径及文件名
29     function SetFileName($fileName30 31         return $this->fileName = 32 33      Open()
34 35         if ($this->handle = fopen($this->fileName,$this->mod))
36             handle;
37         else
38             return 39 40      Close()
41 42         fclose(handle);
43 44     function Write($content45 46         fwrite($this->handle,1)">);
47 48     建立目录,支持绝对路径与相对路径,相对路径相对于当前脚本。linux下注意要有权限
49     function MkDir($pathname50 51         if(){
52             str_replace("","/",1)">53             if(!file_exists()){
54                 if(!@mkdir($pathname,077755                     echo "建立文件夹失败"56                     exit57                 }
58             }
59         }else60             61         }
62 63      生成静态文件
64      Create()
65 66         $tmp = Templet;
67         foreach ($this->DataSource as $key => $value) {
68             $tmp = str_replace( $key,1)">$value,1)">$tmp69 70         MkDir(dirname(fileName));
71         Open();
72         $this->Write(73         Close();
74 75 }
76 ?>

?

二、使用方法
复制以下代码,存为一个php文件,如test.php

include("static.class.php"for($i=0;$i<10;$i++ 4     ob_start();
 5     echo "1234567890".$i;要生成静态页面的内容
 6     $buffer=ob_get_contents 7     ob_end_clean 8     $filename=$i.".html" 9     $filepath=$_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF'])."/a/";//绝对路径
10     $filepath="../a/";相对路径
11     $arr=array("1"=>"a","3"=>"c");标签替换规则
12     CreateShtml($buffer,1)">$filename,1)">$filepath,1)">function CreateShtml(()) {
15     $shtml = new Shtml();
16     $shtml->SetFileName($filepath.$filename17     $shtml->Templet = $buffer18     $shtml->BindData(19     $shtml->Create();
20 21 ?>

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读