Home >php教程 >PHP开发 >Summary of methods for generating HTML pure static web pages from the entire website using PHP

Summary of methods for generating HTML pure static web pages from the entire website using PHP

高洛峰
高洛峰Original
2018-05-14 14:41:317923browse

<?php 
//在你的开始处加入 ob_start(); 
ob_start(); 

//以下是你的代码 
//在结尾加入 ob_end_clean(),并把本页输出到一个变量中 
$temp = ob_get_contents(); 
ob_end_clean(); 

//写入文件 
$fp = fopen(‘文件名&#39;,&#39;w&#39;); 
fwrite($fp,$temp) or die(‘写文件错误&#39;); 
?>

This is just the most basic method, not very practical, because the website needs to be updated and HTML needs to be regenerated regularly
The following is the method I use:

if(file_exists(“xxx.html”)) 
{ 
$time = time(); 

//文件修改时间和现在时间相差半小时一下的话,直接导向html文件,否则重新生成html 
if($time - filemtime(“xxx.html”) < 30*60) 
{ 
header(“Location:xxx.html”); 
} 
} 
//在你的开始处加入 ob_start(); 
ob_start(); 

//页面的详细内容 
//在结尾加入 ob_end_clean(),并把本页输出到一个变量中 
$temp = ob_get_contents(); 
ob_end_clean(); 

//写入文件 
$fp = fopen(‘xxx.html&#39;,&#39;w&#39;); 
fwrite($fp,$temp) or die(‘写文件错误&#39;); 

//重新导向 
header(“Location:xxx.html”);

The following is an introduction to some functions used:
1. Flush: refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format: void ob_start(void)
Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3, ob_get_contents: Return the contents of the internal buffer.
Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; like ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
6. ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
Usage: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it!
7. ob_implicit_flush: Turn on or off absolute refresh
Usage method: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $|=x. This string can open/close the buffer, and The ob_implicit_flush function is the same as that one. The default is to turn off the buffer. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush().

More PHP will generate HTML pure static web pages for the entire website Please pay attention to the PHP Chinese website for related articles summarizing the methods!

Statement:
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