Generate a static html file class based on the template file. Generally, we use PHP to output an html file, always using $ headhead ...... A long string like head. This class provides a simple method to output html files using PHP. This avoids
Generally, we use PHP to output an html file, always using $ head ="......"This is a long string. This class provides a simple method to output html files using PHP. Avoid too many long strings in the program.
The class definition file createhtml. class. php is as follows:
//-------------------
// 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 Start
// Mark End
Function isbegin ($ str ){
$ Pattern =" ";
If (ereg ($ pattern, $ str) return true;
Return false;
}
Function isfinish ($ str ){
$ Pattern =" ";
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.
Note !! This sentence is a separate line
.........
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 ();
?>
Else "head ...... /Head "is a long string. This class provides a simple method to output html files using PHP. To avoid...