Php uses tag replacement to generate static pages
Php can use the built-in function preg_replace to replace arrays in batches. However, regular expression replacement is inefficient and inconvenient to use. For more information, see the php manual. If you have any need, refer.
This Code demonstrates how php generates static pages through custom template pages and custom tags. The principle is very simple, that is, to replace tags on the template page with dynamic data. I hope it will give you some inspiration.
Template.html Template File
?
| 1 2 3 4 5 6 7 8 9 10 |
<! 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> {site_title}-sharejs.com </title> </Head> <Body> <Iframe width = "100%" height = "1000px" scrolling = "yes" frameborder = "0" src = "{site_url}"> </iframe> </Body> </Html> |
Test. php dynamic File
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<? Php Header ('content-type: text/html; charset = UTF-8 '); // prevents generated page garbled characters $ Title = "PHP dynamic generation of static HTML page _ script Sharing Network"; // defines 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 template content $ Str = str_replace ("{penglig_site_title}", $ title, $ str); // replace content $ Str = str_replace ("{penglig_site_url}", $ url, $ str); // replace content Fclose ($ fp ); $ Handle = fopen ($ dest_file, "w"); // write mode to open the file to be written Fwrite ($ handle, $ str); // write the replaced content into the generated HTML file. Fclose ($ handle); // close open files and release file pointers and related buffers. Echo "<script> alert ('generated successfully'); window. location. href = '". $ dest_file. "'; </script> "; ?> |
The above is all the content of this article. I hope you will like it.