基于PHP生成静态页的实现方法_PHP
t1.php
复制代码 代码如下:
// 方法一根据模版生成静态页面
// replaceTemplateString函数用于替换模板中指定字符串
function replaceTemplateString($templateString) {
// 用来替换的变量
$title = "文章标题";
$body = "这里是文章主体";
// 替换模板中指定字符串
$showString = str_replace ( "%title%", $title, $templateString );
$showString = str_replace ( "%body%", $body, $showString );
// 返回替换后的结果
return $showString;
}
$template_file = "template.html";
$new_file = "new.html";
// 模版文件指针
$template_juBing = fopen ( $template_file, "r" );
// 要生成的文件指针
$newFile_juBing = fopen ( $new_file, "w" );
// 方式一获取整体模板内容字符串,替换后赋给新文件
$templateString = fread ( $template_juBing, filesize ( $template_file ) );
$showString = replaceTemplateString ( $templateString ); // 替换模板中字符串
fwrite ( $newFile_juBing, $showString ); // 将替换后的内容写入生成的HTML文件
/*
// 方式二循环读取模版每行内容字符串,替换后依次添加到新文件
while ( ! feof ( $template_juBing ) ) { // feof() 函数检测是否已到达文件末尾。如果文件指针到了末尾或者出错时则返回 TRUE。否则返回FALSE(包括 socket 超时和其它情况)。
$templateString = fgets ( $template_juBing ); // fgets(file,length) 从文件指针中读取一行并返回长度最多为 length - 1 字节长度的字符串,包括换行符。如果没有指定 length,则默认为 1K,或者说 1024 字节。
$showString = replaceTemplateString ( $templateString );
fwrite ( $newFile_juBing, $showString ); // 第一次往打开的指针文件中写入内容时会替换指针文件中原有内容,在该文件指针关闭前,fwrite函数再添加内容会在已添加内容之后
}
*/
// 关闭文件指针
fclose ( $newFile_juBing );
fclose ( $template_juBing );
/*
数据库与静态页的关系
通常数据库内添加一条信息同后,生成一个该信息的静态页面,所以最好在数据库表中添加一字段存储对应静态页面的路径文件名,方便以后的修改,删除
模版的替换
一般来说,如果需要修改静态HTML页面的模版,通常的做法是将所有的已经生成的HTML页面删除,然后重新创建新的HTML页面。(或者说全部重新覆盖生成)
静态页上的动态操作
有些时候,在创建的静态HTML页上面也需要进行一些动态操作。例如,新闻系统中的每篇新闻要统计点击率。
可通过一个宽和高都为0像素的图像控件来隐藏的调用一个php页面来实现页面计数器功能,如
链接目录的静态页
通常对于使用静态页面的系统来说,往往将连接列表的目录页也生成静态HTML文件供访问者浏览
注意的是因为每增加或者减少一条数据库信息都会对链接列表产生影响,因此,每次对数据库信息进行添加和删除时都需要更新链接目录的静态页。
分页的设计可以通过创建多个链接目录的静态页来完成。
*/
// 方法二根据缓冲区生成
ob_start (); // 当缓冲区激活时,并且有ob_end_clean()的情况下,所有输出打印的非文件头信息均不会输出打印到页面,而是保存在内部缓冲区。如果没有ob_end_clean(),则信息既被存在内部缓冲区,也被输出打印
?>
this is test Output Control
echo "
this is test Output Control
";
include_once 'cache/newFile.php';
$contents = ob_get_contents (); // 获取缓冲区到此为止存储的信息,缓冲区只保存会向页面浏览器输出打印的内容,php执行代码等不会保存
// $contents = ob_get_clean(); // 获取缓冲区到此为止存储的信息,并关闭清除缓冲区
// ob_end_flush();//输出打印缓冲区到此为止存储的信息,并关闭清除缓冲区
ob_end_clean (); // 关闭清除缓冲区的内容
file_put_contents ( $new_file, $contents );// 向文件写入内容
?>
template.html
复制代码 代码如下:
%title%
%body%

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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





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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
