PHP can use the built-in function preg_replace to batch replace with arrays, but regular expression replacement is inefficient and difficult to use. inconvenient. Please refer to the PHP manual for details. Friends in need can refer to it.
This code demonstrates how PHP generates static pages through customized template pages and custom tags. The principle is very simple, just replace the tags in the template page with dynamic data. Hope it can give you some inspiration.
template.html template file
?
2 3
4 56 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <🎜>header('content-type:text/html; charset=utf-8');//Prevent the generated page from being garbled<🎜> <🎜>$title = "PHP dynamically generates static HTML pages_script sharing network"; //Define variables<🎜> <🎜>$url = "http://www.sharejs.com";<🎜> <🎜>$temp_file = "temp.html"; //Temporary file, which can also be a template file<🎜> <🎜>$dest_file = "dest_page.html"; //Generated target page<🎜> <🎜>$fp = fopen($temp_file, "r"); //Read-only open template<🎜> <🎜>$str = fread($fp, filesize($temp_file));//Read the content in the template<🎜> <🎜>$str = str_replace("{penglig_site_title}", $title, $str);//Replacement content<🎜> <🎜>$str = str_replace("{penglig_site_url}", $url, $str);//Replacement content<🎜> <🎜>fclose($fp);<🎜> <🎜>$handle = fopen($dest_file, "w"); //Open the file to be written in writing mode<🎜> <🎜>fwrite($handle, $str); //Write the content just replaced into the generated HTML file<🎜> <🎜>fclose($handle);//Close the open file and release the file pointer and related buffer<🎜> <🎜>echo "<script>alert('Generation successful');window.location.href='".$dest_file."';</script>"; ?> |