PHP can be preg_replace with the function of the array, but the use of regular expression replacement efficiency is very low, it is not convenient to use. Specific reference to the PHP manual. The need for small partners can refer to the following.
This code demonstrates how PHP generates static pages from custom template pages and custom tags. The principle is very simple, is to replace the label in the template page with Dynamic Data. I hope I can 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 "> |
test.php Dynamic Files
?
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 ');//Prevent generated page garbled $title = "PHP dynamic generation static HTML page _ script sharing network"; Define variable $url = "http://www.sharejs.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 content in Template $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 to open the file that needs to be written fwrite ($handle, $STR); Write the content just replaced into the generated HTML file fclose ($handle);//Close Open file, release file pointer and associated buffer echo "<script>alert" (' Build success '); Window.location.href= ' ". $dest _file." '; </script> ";?> |
The above mentioned is the entire content of this article, I hope you can enjoy.