• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php静态化类

php 搞代码 4年前 (2022-01-23) 14次浏览 已收录 0个评论

一、静态化类
复制以下代码,存为文件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,$filepath11 * example:12 */13 class Shtml14 {15     function Shtml()16     {17         $this->Templet      = "";18         $this->DataSource   = array();    19         $this->fileName     = "";20         $this->mod          = "wb";21         $this->handle       = false;22     }23     //绑定数据源,参数为一数组24     function BindData($arr)25     {26         $this->DataSource = $arr;27     }28     //设置路径及文件名29     function SetFileName($fileName)30     {31         return $this->fileName = $fileName;32     }33     function Open()34     {35         if ($this->handle = fopen($this->fileName, $this->mod))36             return $this->handle;37         else38             return false;39     }40     function Cl<p>5本文来源gao!daima.com搞$代!码#网#</p><pre>搞代gaodaima码

ose()41 {42 return fclose($this->handle);43 }44 function Write($content)45 {46 return fwrite($this->handle, $content);47 }48 //建立目录,支持绝对路径与相对路径,相对路径相对于当前脚本。linux下注意要有权限49 function MkDir($pathname)50 {51 if($pathname){52 str_replace(“\\”, “/”, $pathname);53 if(!file_exists($pathname)){54 if(!@mkdir($pathname,0777)){55 echo “建立文件夹失败”;56 exit;57 }58 }59 }else{60 return false;61 }62 }63 // 生成静态文件64 function Create()65 {66 $tmp = $this->Templet;67 foreach ($this->DataSource as $key => $value) {68 $tmp = str_replace( $key , $value, $tmp);69 }70 $this->MkDir(dirname($this->fileName));71 $this->Open();72 $this->Write($tmp);73 $this->Close();74 }75 }76 ?>

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

 1 <?php 2 include("static.class.php"); 3 for($i=0;$i"a","3"=>"c");//标签替换规则12     CreateShtml($buffer,$filename,$filepath,$arr);13 }14 function CreateShtml($buffer,$filename,$filepath,$arr=array()) {15     $shtml = new Shtml();16     $shtml->SetFileName($filepath.$filename);17     $shtml->Templet = $buffer;18     $shtml->BindData($arr);19     $shtml->Create();20 }21 ?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php静态化类
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址