Replace function is used to Replace the keywords in the content read from the template file with the content in the variable template.htm:
The code is as follows:
% Title %
% Title %
%body%
Php file:
The code is as follows:
// 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
The code is as follows:
Article title
Article title
Here is the subject of the article