Home > Backend Development > PHP Tutorial > Summary of some methods for generating static html files with PHP_PHP Tutorial

Summary of some methods for generating static html files with PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:43:33
Original
830 people have browsed it

Using PHP to generate static files, what we use most is to replace them in the form of templates. For example, I define {A} as a tag. In PHP, I only need to use fopen to read the template file, and then use replace to replace {A}. Just generate a static html file instance for PHP.

1, here is a way to use templates!

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

$fp = fopen ("templets.html","a");
if ($fp){
$fup = fread ($fp,filesize("templets.html"));
$fp2 = fopen ("html.shtml","w");
if ($fwrite ($fp2,$fup)){
$fclose ($fp);
$fcolse ($fp2);
die ("写入模板成功");
} else {
fclose ($fp);
die ("写入模板失败!");
}
}
?>

$fp = fopen ("templets.html","a");

if ($fp){

$fup = fread ($fp,filesize("templets.html"));
$fp2 = fopen ("html.shtml","w");

if ($fwrite ($fp2,$fup)){
代码如下 复制代码
$content = "这是一个以日期时间为文件名的静态生成网页的测试文件,
文件名格式一般为年月日时分秒.html";
$date = date('YmdHis');
$fp = fopen (date('YmdHis') . '.html',"w");
//本函数可用来打开本地或者远端的文件 'w' 开文件方式为写入,
文件指针指到开始处,并将原文件的长度设为 0。若文件不存在,
则建立新文件。
if (fwrite ($fp,$content)){
//格式是.int fwrite(int fp(文件名), string string(内容),
 int [length](长度));本函数将字符串 string 写入文件资料流的指针 fp 上。
若有指定长度 length,则会写入指定长度字符串,或是写到字符串结束。
fclose ($fp);//函数用来关闭已经打开的文件的指针 fp。
成功返回 true,失败则返回 false。
die ("写入模板成功");
} else {
fclose ($fp);
die ("写入模板失败!");
}
echo ($content);
?>
$fclose ($fp);

$fcolse ($fp2);

die ("Write template successfully");
 代码如下 复制代码

$s_fname = "93e.php";
$o_fname = "93e.htm";
ob_end_clean();
ob_start();
include($s_fname);
$length = ob_get_length();
$buffer = ob_get_contents();
$buffer = eregi_replace("r","",$buffer);
ob_end_clean();

$fp = fopen($o_fname,"w+");
fwrite($fp,$buffer);
fclose($fp);
?>

} else { fclose ($fp); die ("Failed to write template!"); } } ?> Simply write the template into a file and save it as html.html 2. Generate html file name according to time
The code is as follows Copy code
$content = "This is a test file for statically generated web pages with date and time as the file name, <🎜> The file name format is generally year, month, day, hour, minute and second.html"; $date = date('YmdHis'); $fp = fopen (date('YmdHis') . '.html',"w"); //This function can be used to open local or remote files 'w'. The file opening method is writing, The file pointer points to the beginning and the length of the original file is set to 0. If the file does not exist, A new file is created. if (fwrite ($fp,$content)){ //The format is .int fwrite(int fp(file name), string string(content), int [length](length)); This function writes the string string to the pointer fp of the file data stream. If length is specified, the string of the specified length will be written, or written to the end of the string. fclose ($fp);//The function is used to close the pointer fp of the opened file. Returns true on success, false on failure. die ("Write template successfully"); } else { fclose ($fp); die ("Failed to write template!"); } echo ($content); ?>
3. The following is a method for converting file names
The code is as follows Copy code
$s_fname = "93e.php"; <🎜> $o_fname = "93e.htm"; <🎜> ob_end_clean(); <🎜> ob_start(); <🎜> include($s_fname); <🎜> $length = ob_get_length(); <🎜> $buffer = ob_get_contents(); <🎜> $buffer = eregi_replace("r","",$buffer); <🎜> ob_end_clean(); <🎜> <🎜> $fp = fopen($o_fname,"w+"); <🎜> fwrite($fp,$buffer); <🎜> fclose($fp); <🎜> ?>

In this way, 93e.php can be converted into a static HTML file. It should be noted that there cannot be ob_end_clean(); and ob_start(); statements in the file to be converted, and the directory must have write permission.

The above three methods all generate html pages that need to be updated every time. Next, we can use dynamic page caching technology to instantiate html+php pages. This method is better than the above

First set up the .htaccess file to convert dynamically called parameters into static HTML URL addresses. For example, forward files in the post directory to the wp-post.php file in the root directory. Add statements similar to:

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

RewriteRule ^post/([a-z0-9-]+.html)$  wp-post.php?

Copy code

 代码如下 复制代码

ob_start();
$qstring = isset($_SERVER["QUERY_STRING"]) ? $_SERVER["QUERY_STRING"] : "";
define("HTML_FILE",  $_SERVER['DOCUMENT_ROOT']."/post/".$qstring);

if (file_exists(HTML_FILE))
{
 $lcft = filemtime(HTML_FILE);
 if (($lcft + 3600) > time())  //判断上次生成HTML文件是否超过1小时,若没有才直接输出文件内容
 {
  echo(file_get_contents(HTML_FILE));
  exit(0);
 }
}

RewriteRule ^post/([a-z0-9-]+.html)$ wp-post.php?$1$2

 代码如下 复制代码

define("HTMLMETA","");
$buffer = ob_get_flush();
$fp = fopen(HTML_FILE, "w");
if ($fp)
{
 fwrite($fp, $buffer.HTMLMETA);
 fclose($fp);
}

Then modify the wp-post.php file and add the following PHP code at the beginning of the file:
The code is as follows
Copy code

ob_start();
$qstring = isset($_SERVER["QUERY_STRING"]) ? $_SERVER["QUERY_STRING"] : "";
define("HTML_FILE", $_SERVER['DOCUMENT_ROOT']."/post/".$qstring);

After is the existing PHP code, and then add the following PHP code at the end of the current code:
The code is as follows Copy code
define("HTMLMETA","");
$buffer = ob_get_flush();
$fp = fopen(HTML_FILE, "w");
if ($fp)
{
fwrite($fp, $buffer.HTMLMETA);
fclose($fp);
}
Okay, then check your static HTML page. If a comment line appears at the end of the page, it means that the static HTML file has been successfully created
http://www.bkjia.com/PHPjc/633160.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633160.htmlTechArticleUsing PHP to generate static files. What we use most is to replace the generated files in the form of templates. For example, I put {A } is defined as a tag in php. Just use fopen to read the template file, and then use repla...
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