Template.htm:
CopyCode The Code is as follows: <HTML>
<Head>
<Title> % title % </title>
</Head>
<Body>
<H1> % title % <HR>
<PRE> % body % </PRE>
</Body>
</Html>
PHP file: copy Code the code is as follows: // replace function is used to replace the keywords in the content read from the template file with the content in the variable.
function Replace ($ row)
{< br> // defines the variable to replace
$ Title = " Article title ";
$ body = "here 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 result after replacement
return $ row;
}< br> // 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)
{< br> $ 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
}< br> // close the file pointer
fclose ($ f_new );
fclose ($ f_tem);
?>
Generate a new HTML page: new.htmlCopy codeThe Code is as follows: <HTML>
<Head>
<Title> Article Title </title>
</Head>
<Body>
<H1> Article Title <HR>
<PRE> This is the subject of the article </PRE>
</Body>
</Html>