Use the Replace function, namely the str_replace function of php, to Replace the keywords in the content read in the template file with the content in the variable, thus achieving simple template separation. Use the Replace function, namely the str_replace function of php, to Replace the keywords in the content read in the template file with the content in the variable, thus achieving simple template separation.
Template.htm:
Php file:
// Replace function is used to Replace the keyword in the content read from the template file with the content in the variable Function Replace ($ row) { // Define the variable to replace $ Title = "article title "; $ Body = "this is the subject of the article "; // Replace the keyword in the parameter $ Row = str_replace ("% title %", $ title, $ row ); $ Row = str_replace ("% body %", $ body, $ row ); // Return the replaced result Return $ row; } // Template file pointer $ F_tem = fopen ("template.htm", "r "); // Generated file pointer $ F_new = fopen ("new.htm", "w "); // Read the template file cyclically and read a row each time While (! Feof ($ f_tem )) { $ Row = fgets ($ f_tem ); $ Row = Replace ($ row); // Replace the keyword in the read content Fwrite ($ f_new, $ row); // write the replaced content to the generated HTML file } // Close the file pointer Fclose ($ f_new ); Fclose ($ f_tem ); ?> |
Generate a new html page: new.html
Article title
Article title
Here is the subject of the article
|