PHP dynamically generates static HTML page sample code

  • 2020-12-10 00:39:42
  • OfStack

temp.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{penglig_site_title}</title>
</head>
<body>
<iframe width="100%" height="1000px" scrolling="yes" frameborder="0" src="{penglig_site_url}" ></iframe>
</body>
</html>

test.php


<?php
header('content-type:text/html; charset=utf-8');// Prevent generated page clutter 
$title = "PHP  Dynamically generated static state HTML page _ The home of the script "; // Define variables 
$url = "https://www.ofstack.com/";
$temp_file = "temp.html"; // Temporary files can also be template files 
$dest_file = "dest_page.html"; // The generated target page 
$fp = fopen($temp_file, "r"); // Read-only open template 
$str = fread($fp, filesize($temp_file));// Read the contents of the template 
$str = str_replace("{penglig_site_title}", $title, $str);// Alternative content 
$str = str_replace("{penglig_site_url}", $url, $str);// Alternative content 
fclose($fp);
$handle = fopen($dest_file, "w"); // Write mode opens the file that needs to be written 
fwrite($handle, $str); // I'm just going to put what I just replaced into the generated HTML file 
fclose($handle);// Closes the open file, frees the file pointer and the associated buffer 
echo "<script>alert(' Generate success ');window.location.href='".$dest_file."';</script>";
?>

Run test.php and you are ready to demonstrate. The specific code is modified according to the actual requirements.


Related articles: