Home  >  Article  >  Backend Development  >  Three ways to parse PHP to generate static html files_PHP tutorial

Three ways to parse PHP to generate static html files_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:05:09755browse

This article will introduce three methods for generating static html files in Php.
1, here is a way to use templates!

Copy the code The code is as follows:

$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 ("Write template successfully");
} else {
fclose ($fp);
die ("Failed to write template!");
}
}
?>
Simply write the template into a file and save it as html.html

2, generate html files according to time Name
Copy code The code is as follows:

$content = "This is a The date and time are test files for statically generated web pages.
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 file 'w' The file mode 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,
creates a new file.
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 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 and false on failure.
die ("Write template successfully");
} else {
fclose ($fp);
die ("Failed to write template!");
}
echo ($content);
?>

3. The following is a method of converting file names
Copy code The code is as follows:

$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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327708.htmlTechArticleThis article will introduce three methods for generating static html files in Php. 1. Here is a way to use templates! Copy the code as follows: ?php $fp = fopen ("templets.html","a"); if ($fp){ $fup =...
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