Template file template.htm:
Copy Code code as follows:
<title>%title%</title>
<body>
<H1>%title%</H1>
<pre>%body%</pre>
</body>
PHP File:
Copy Code code as follows:
<?php
The Replace function is used to replace the keyword in the content read from the template file with the contents of the variable
function Replace ($row)
{
Define 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);
Returns the result of a replacement
return $row;
}
Template file Pointers
$f _tem = fopen ("template.htm", "R");
Generated file pointers
$f _new = fopen ("new.htm", "w");
Iterate through the template file, reading one row at a time
while (!feof ($f _tem))
{
$row = fgets ($f _tem);
$row = Replace ($row); Replace the keyword in the read content
Fwrite ($f _new, $row); Writes the replaced content to the generated HTML file
}
Close file pointer
Fclose ($f _new);
Fclose ($f _tem);
?>
To generate a new HTML page: new.html
Copy Code code as follows:
<title> article title </title>
& L T;/head>
<body>
<H1> article title </H1>
<pre> This is the article main </pre>
</body>