Php uses tag replacement to generate static pages

Source: Internet
Author: User

Php uses tag replacement to generate static pages

Php can use the built-in function preg_replace to replace arrays in batches. However, regular expression replacement is inefficient and inconvenient to use. For more information, see the php manual. If you have any need, refer.

This Code demonstrates how php generates static pages through custom template pages and custom tags. The principle is very simple, that is, to replace tags on the template page with dynamic data. I hope it will give you some inspiration.

Template.html Template File

?

1

2

3

4

5

6

7

8

9

10

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> {site_title}-sharejs.com </title>

</Head>

<Body>

<Iframe width = "100%" height = "1000px" scrolling = "yes" frameborder = "0" src = "{site_url}"> </iframe>

</Body>

</Html>

Test. php dynamic File

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<? Php

Header ('content-type: text/html; charset = UTF-8 '); // prevents generated page garbled characters

$ Title = "PHP dynamic generation of static HTML page _ script Sharing Network"; // defines Variables

$ Url = "http://www.sharejs.com ";

$ Temp_file = "temp.html"; // temporary file, which can also be a template file

$ Dest_file = "dest_page.html"; // generated target page

$ Fp = fopen ($ temp_file, "r"); // read-only open Template

$ Str = fread ($ fp, filesize ($ temp_file); // read the template content

$ Str = str_replace ("{penglig_site_title}", $ title, $ str); // replace content

$ Str = str_replace ("{penglig_site_url}", $ url, $ str); // replace content

Fclose ($ fp );

$ Handle = fopen ($ dest_file, "w"); // write mode to open the file to be written

Fwrite ($ handle, $ str); // write the replaced content into the generated HTML file.

Fclose ($ handle); // close open files and release file pointers and related buffers.

Echo "<script> alert ('generated successfully'); window. location. href = '". $ dest_file. "'; </script> ";

?>

The above is all the content of this article. I hope you will like it.

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.