Simple principles and methods for generating static HTML in batches using PHP

Source: Internet
Author: User
Php html generation technology is simpler than ASP technology. the following briefly describes how to generate HTML in batches using PHP. Currently, CMS supports generating HTML in articles, the benefits of generating HTML static web pages do not need to be discussed. Both the speed of page opening and SEO optimization are significantly improved. sometimes there are more or less vulnerabilities in web pages, converting PHP to HTML format also effectively protects websites.

The HTML generation technology in PHP is simpler than that in ASP. the following briefly describes how to generate HTML in batches in PHP. let's take a look at the source code:

The code is as follows:
If ($ _ POST [button]) {

$ SQL = "select * from doorsjk_about where id <12 ";
$ Rs = $ db-> query ($ SQL );
While ($ row = $ db-> fetch_array ($ rs )){

Using path='list12'.w.row?id=.'.html ';

$ Fp = fopen ("../group/about.html", "r ");
$ Str = fread ($ fp, filesize ("../group/about.html "));
$ Str = str_replace ("{title}", $ row [title], $ str );
$ Str = str_replace ("{name}", $ system ['name'], $ str );
$ Str = str_replace ("{content}", $ row [content], $ str );
$ Str = str_replace ("{copyright}", $ system ['copyright'], $ str );
Fclose ($ fp );

$ Handle = fopen ("../group/". $ path, "w ");
Fwrite ($ handle, $ str );
Echo "generating ";
Echo "$ path ";
Fclose ($ handle );
Echo "script alert ('generated successfully! '); Window. location. href = 'onlyhtml. php'; script ";

First, the code for connecting to the database is included. The following is a click form. when the button is received, HTML is generated cyclically. the generated pathpattern 'list12).w.row+id=.'.html 'is the name of the generated file, and $ fp = fopen ("... /group/about.html "," r "); is the opened template file. you only need to make the desired HTML file. here, use the str_replace function to replace the tag, in this way, you can call tags similar to {title} in HTML to generate HTML files. In other words, simple HTML is generated in batch.

Call the tag {title} in the HTML file to directly tune it to the file $ row [title] in the read Database. if the data volume is large, it can be generated in batches in segments, is to generate ID: from (). Use limit to accept calls. As for the dynamic files used in HTML, such as generating static files and counting the number of articles read, this dynamic call can be encapsulated into JS files and embedded.

Here we will introduce the batch re-generation. In fact, HTML files are generated when news is entered, and HTML files are generated when the content of the article is edited. In fact, the principles are similar. I will write them down for reference.

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: 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.