Generate static (html) page program code from php_PHP tutorial

WBOY
Release: 2016-07-20 11:11:27
Original
838 people have browsed it

Generating static pages generally involves generating HTML pages from dynamic pages. This can reduce server load and is also a commonly used optimization method for major websites. Now I will share a class for generating static (html) pages from PHP.

The code is as follows
 代码如下 复制代码

 

class create_html {

private $template;

//模版

private $file_name;

//文件名

private $array;

//数据数组

function __construct($file_name, $template, $array) {

//构造类

$this->template = $this->read_file($template, "r");

//读取模板文件

$this->file_name = $file_name;

$this->array = $array;

//数据数据

$this->html();

//生成html

}

function html() {

//生成html

while (ereg ("{([0-9]+)}", $this->template, $regs)) {

//循环模版中所能的{1}…..

$num = $regs[1];

//得到1、2、3序列

$this->template = ereg_replace("{".$num."}", $this->array[$num], $this->template);

//把数据替换成html内容

$this->write_file($this->file_name, $this->template, "w+");

//生成HTML文件

}

}

function read_file($file_url, $method = "r") {

//读取文件

$fp = @fopen($file_url, $method);

//打开文件

$file_data = fread($fp, filesize($file_url));

//读取文件信息

return $file_data;

}

function write_file($file_url, $data, $method) {

//写入文件

$fp = @fopen($file_url, $method);

//打开文件

@flock($fp, LOCK_EX);

//锁定文件

$file_data = fwrite($fp, $data);

//写入文件

fclose($fp);

//关闭文件

return $file_data;

}

}

#例子———————-

#读取邮件回复模版———————————————————————————-

$title = "标题";

$navigation = "浏览器";

$happy_origin = "作者";

$name = "test2.htm";

$template = "default_tmp.php";

//模版中用{1}{2}来替换

$daytype = array(1 => $title,

2 => $navigation,

3 => $happy_origin);

$htm = new Restore_email($template, $daytype);

echo $htm->pint();

?>

Copy code

class create_html {private $template;//Templateprivate $ file_name;//File nameprivate $array;//Data array

function __construct($file_name, $template, $array) {
//Construction class
<🎜>$this->template = $this->read_file($template, "r");//Read template File$this->file_name = $file_name;$this->array = $array;//data data$ this->html();//Generate html}function html() {//Generate htmlwhile (ereg ("{([0-9]+)}", $this->template, $regs)) {//{1} that can be looped in the template…. .$num = $regs[1];//Get the 1, 2, 3 sequence$this->template = ereg_replace("{". $num."}", $this->array[$num], $this->template);//Replace the data with html content$this-> ;write_file($this->file_name, $this->template, "w+");//Generate HTML file}} function read_file($file_url, $method = "r") {//Read file$fp = @fopen($file_url, $method); //Open file$file_data = fread($fp, filesize($file_url));//Read file informationreturn $file_data; }function write_file($file_url, $data, $method) {//Write file$fp = @fopen($ file_url, $method);//Open file@flock($fp, LOCK_EX);//Lock file$file_data = fwrite($fp, $data);//Write filefclose($fp);//Close filereturn $ file_data;}}#Example————————-#Read email reply template—————— ——————————————————————-$title = "Title";$navigation = "Browser";$happy_origin = "Author";$name = "test2.htm";$template = "default_tmp.php";// Use {1}{2} in the template to replace $daytype = array(1 => $title,2 => $navigation,3 = > $happy_origin);$htm = new Restore_email($template, $daytype);echo $htm->pint();?> http://www.bkjia.com/PHPjc/444648.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444648.htmlTechArticleGenerating static pages generally generates dynamic pages into html pages, which can reduce the server load and is now commonly used by major websites. Optimization method, let me share a way to generate static php...
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template