php使用标签替换的方式生成静态页面_PHP
这段代码演示了php如何通过自定义的模板页面和自定义标签生成静态页面。原理非常简单,就是将模板页面中的标签替换成动态数据即可。希望能给你一定的启发。
template.html 模板文件
<!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 动态文件
<?php header('content-type:text/html; charset=utf-8');//防止生成的页面乱码 $title = "PHP 动态生成静态HTML页面_脚本分享网"; //定义变量 $url = "http://www.sharejs.com"; $temp_file = "temp.html"; //临时文件,也可以是模板文件 $dest_file = "dest_page.html"; //生成的目标页面 $fp = fopen($temp_file, "r"); //只读打开模板 $str = fread($fp, filesize($temp_file));//读取模板中内容 $str = str_replace("{penglig_site_title}", $title, $str);//替换内容 $str = str_replace("{penglig_site_url}", $url, $str);//替换内容 fclose($fp); $handle = fopen($dest_file, "w"); //写入方式打开需要写入的文件 fwrite($handle, $str); //把刚才替换的内容写进生成的HTML文件 fclose($handle);//关闭打开的文件,释放文件指针和相关的缓冲区 echo "<script>alert('生成成功');window.location.href='".$dest_file."';</script>"; ?>
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
