Generally, we use PHP to output an html file, always using $ head = "
The class definition file createhtml. class. php is as follows:
<? Php
//-------------------
// TCreateHTML
// Generate a static html file Class Based on the template file
// Author: sharetop
// Email: ycshowtop@21cn.com
//-------------------
// ***** Define the required function
// Convention to mark <! -- ## Name ##--> is the start
// Mark <! -- @ Name @ --> is the end
Function isbegin ($ str ){
$ Pattern = "<! -- # [A-zA-Z] + # --> ";
If (ereg ($ pattern, $ str) return true;
Return false;
}
Function isfinish ($ str ){
$ Pattern = "<! -- @ [A-zA-Z] + @ --> ";
If (ereg ($ pattern, $ str) return true;
Return false;
}
Function getname ($ str ){
$ Tmp = explode ("#", $ str );
Return $ tmp [1];
}
//******************
// ******* Define a class
Class TCreateHTML {
Var $ HTemplate;
Var $ FileName;
Var $ ModiString;
// ******** Interface function
// Construct a template
Function TCreateHTML ($ tmplate ){
$ This-> HTemplate = $ tmplate;
}
// Set the output file name
Function SetHTML ($ filename ){
$ This-> FileName = $ filename;
}
// Set the tag name and the replaced string
Function EditableBlock ($ name, $ replace ){
$ This-> ModiString [$ name] = $ replace;
}
// Write an HTML file
Function WriteHtml (){
$ Fc = file ($ this-> HTemplate );
$ Fp = fopen ($ this-> FileName, "w ");
$ K = count ($ fc );
$ Begin = false;
$ First = false;
$ Tag = "";
For ($ I = 0; $ I <$ k; $ I ++ ){
If (isbegin ($ fc [$ I]) {
Fputs ($ fp, $ fc [$ I]);
$ Begin = true;
$ First = true;
$ Tag = getname ($ fc [$ I]);
Continue;
}
If (isfinish ($ fc [$ I]) {
Fputs ($ fp, $ fc [$ I]);
$ Begin = false;
$ First = false;
$ Tag = "";
Continue;
}
If ($ begin = true ){
If ($ first = true ){
$ Fc [$ I] = $ this-> ModiString [$ tag]. "";
$ First = false;
}
Else $ fc [$ I] = "";
}
Fputs ($ fp, $ fc [$ I]);
}
Fclose ($ fp );
}
// -------- Class end
}
?>
Example:
(
Create an html file first, and add the tag where you want to replace it.
<! -- Aaa --> note !! This sentence is a separate line
.........
<! -- @ Aaa --> note !! This sentence is a separate line
And so on
)
<?
Require "createhtml. class. php ";
$ Chtml = new TCreateHTML ("template.htm ");
$ Chtml-> SetHTML ("news.htm ");
$ Chtml-> EditableBlock ("aaa", "11aa111aa ");
$ Chtml-> EditableBlock ("bbb", "11bbb122bb ");
$ Chtml-> EditableBlock ("ccc", "11cc333cc ");
$ Chtml-> WriteHtml ();
?>