Generate a static html file class based on the template file _ PHP Tutorial

Source: Internet
Author: User
Tags ereg
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.