Home > Backend Development > PHP Tutorial > Two ways to generate html static pages in php_PHP tutorial

Two ways to generate html static pages in php_PHP tutorial

WBOY
Release: 2016-07-20 11:11:21
Original
911 people have browsed it

In the articles I have seen before, they either use code to fill up space or use the language used by experts to communicate with experts to scare newcomers away. Therefore, this article tries to explain the overall idea in detail.

The two methods are briefly explained as follows:

First, use PHP's output control function (Output Control) to obtain the static page string, and then write it to a new file.

Instructions for use:

1. Instantiation

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

$cache = new Cache();2、设置缓存时间和缓存目录

$cache = new Cache(60, '/any_other_path/');

$cache = new Cache(); 2. Set cache time and cache directory


$cache = new Cache(60, '/any_other_path/');

The first parameter is the cache seconds, and the second parameter is the cache path , configure as needed.

By default, the cache time is 3600 seconds, and the cache directory is cache/
 代码如下 复制代码

$value = $cache->get('data_key');4、写入缓存

$value = $cache->put('data_key', 'data_value');完整实例:

$cache = new Cache();

//从缓存从读取键值 $key 的数据
$values = $cache->get($key);

//如果没有缓存数据
if ($values == false) {
 //insert code here...
 //写入键值 $key 的数据
 $cache->put($key, $values);
} else {
 //insert code here...
}
Cache.class.php

class Cache {
private $cache_path;//path for the cache
private $cache_expire;//seconds that the cache expires

//cache constructor, optional expiring time and cache path
public function Cache($exp_time=3600,$path="cache/"){
$this->cache_expire=$exp_time;
  $this->cache_path=$path;
 }

 //returns the filename for the cache
 private function fileName($key){
  return $this->cache_path.md5($key);
 }

 //creates new cache files with the given data, $key== name of the cache, data the info/values to store
 public function put($key, $data){
  $values = serialize($data);
  $filename = $this->fileName($key);
  $file = fopen($filename, 'w');
     if ($file){//able to create the file
         fwrite($file, $values);
         fclose($file);
     }
     else return false;
 }

 //returns cache for the given key
 public function get($key){
  $filename = $this->fileName($key);
  if (!file_exists($filename) || !is_readable($filename)){//can't read the cache
   return false;
  }
  if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired
   $file = fopen($filename, "r");// read data file
         if ($file){//able to open the file
             $data = fread($file, filesize($filename));
             fclose($file);
             return unserialize($data);//return the values
         }
         else return false;
  }
  else return false;//was expired you need to create new
  }
}
?>

3. Read cache
The code is as follows Copy code
$value = $cache->get(' data_key'); 4. Write cache $value = $cache->put('data_key', 'data_value'); Complete example: $cache = new Cache() ;//Read the data of key value $key from cache$values ​​= $cache->get($key);//If there is no cached dataif ($values ​​== false) { //insert code here... //Write the data of key value $key $cache->put($key, $values );} else { //insert code here...}Cache.class.phpclass Cache {<🎜> private $cache_path;//path for the cache<🎜> private $cache_expire;//seconds that the cache expires<🎜><🎜> //cache constructor, optional expiring time and cache path<🎜> public function Cache($exp_time =3600,$path="cache/"){<🎜> $this->cache_expire=$exp_time; $this->cache_path=$path; } // returns the filename for the cache private function fileName($key){ return $this->cache_path.md5($key); } //creates new cache files with the given data, $key== name of the cache, data the info/values ​​to store public function put($key, $data){ $values ​​= serialize($data); $filename = $this->fileName($key); $file = fopen($filename, 'w'); if ($file){//able to create the file fwrite ($file, $values); fclose($file); } else return false; } //returns cache for the given key public function get($key){ $filename = $this->fileName($key); if (!file_exists($filename) || !is_readable($filename)){//can't read the cache return false; } if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired $file = fopen($filename, "r");// read data file if ($file){//able to open the file $data = fread($file, filesize($filename) ); fclose($file); return unserialize($data);//return the values } else return false; was expired you need to create new }}?>


2. Use templates to generate

What is a template? If you have used "Save as Template" in Dreamwerver, you should know that templates are used to unify styles. It only allows you to modify a certain part of the page. Of course, this "certain part" is determined by you. This is what the template mentioned in this article means. (In addition, PHP template technology also includes phplib, smarty, etc., which is not the content of this article)

Combining the concept of templates with this article to be more specific is: the artist first makes a page, and then we Use this page as a template (it should be noted that there is no need to use code like EditRegion3 for this template. This code is a logo made by Dreamwerver to facilitate its own design). Use an HTML code in the place we need to change in this template. Replace with characters that can be distinguished, such as "{title}", "[title]". When generating a static page, you only need to replace the data with these strings. This is what templates are about.

Steps:

1. Create a new php page and an html page [template page]; Note: If the data is called from the database, the data will be Save the form and then generate it in a loop;
2. In the php page, open the html page -> Read the content of the html page -> Replace parameters -> Create (open) a new html page -> Replace The content is written into the new file->Close the new file->Generation successful;

$content = fread($open,filesize("template.htm")); //Read the template file content//print_r($content);
The code is as follows
 代码如下 复制代码

$open = fopen("template.htm","r"); //打开模板文件
$content = fread($open,filesize("template.htm")); //读取模板文件内容
//print_r($content);
$content = str_replace("{title}","测试标题",$content);//替换
$content = str_replace("{contents}","测试内容",$content);

$newtemp = fopen("1.htm","w");//生成,用写入方式打开一个不存在(新)的页面
fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
fclose($newtemp);
echo "生成";

Copy code

 代码如下 复制代码

//假设从数据库中调的数据存放在二维数组$arr中
$arr = array(array("新闻标题一","新闻内容一"),array("新闻标题二","新闻内容二")); 

foreach($arr as $key=>$value){
 $title = $value[0];
 $contents = $value[1];
 //echo $title.''.$contents.'';
 $path = $key.'.html';
 $open = fopen("template.htm","r"); //打开模板文件
 $handle = fread($open,filesize("template.htm")); //读取模板文件内容

 $content = str_replace("{title}",$title,$handle);//替换
 $content = str_replace("{contents}",$contents,$handle);

 $newtemp = fopen($path,"w");//用写入方式打开一个不存在(新)的页面
 fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
 fclose($newtemp);
 echo "生成";
}

$open = fopen("template.htm","r"); //Open the template file
$content = str_replace("{title}", "Test title",$content);//Replace
$content = str_replace("{contents}","Test content",$content);

$newtemp = fopen("1.htm ","w");//Generate, open a non-existent (new) page in writing mode

fwrite($newtemp,$content);//Write the content just replaced into a new fileecho "Generate"; php batch generate html test:
The code is as follows Copy code
//Assume that it is transferred from the database The data is stored in the two-dimensional array $arr$arr = array(array("News Title One", "News Content One"), array("News Title Two", "News Content Two")); foreach($arr as $key=>$value){
$title = $value[0];
$contents = $value[1]; //echo $ title.''.$contents.''; $path = $key.'.html'; $open = fopen("template.htm","r"); //Open the template file $handle = fread($open,filesize("template.htm")); //Read the template file content $content = str_replace("{title}",$title,$handle) ;//Replace $content = str_replace("{contents}",$contents,$handle); $newtemp = fopen($path,"w");//Use writing method Open a non-existent (new) page fwrite($newtemp,$content);//Write the just replaced content into a new file fclose($newtemp); echo "Generate"; } http://www.bkjia.com/PHPjc/444662.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444662.htmlTechArticleIn the articles I have seen before, they either use code to fill the space or use the language used by experts to communicate with experts. Newcomers are disappointed. Therefore, this article tries to explain the overall idea in detail. Two kinds...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template