模板文件template.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= $pageTitle ?></title>
</head>
<body>
<?= $content ?>
</body>
</html>
PHP脚本
<?php
$pageTitles = ['Page1', 'Page2', 'Page3']; // 定义页面标题列表
$contents = [
'<h1>Content for Page1</h1>',
'<p>Content for Page2</p>',
'<div class="container"><h2>Content for Page3</h2></div>'
]; // 定义每个页面对应的内容
foreach ($pageTitles as $index => $pageTitle) {
ob_start(); // 开始输出控制
$content = $contents[$index];
require('template.html'); // 加载模版文件
$output = ob_get_clean(); // 获取输出结果
file_put_contents($pageTitle . '.html', $output); // 将输出结果写入到指定名称的 HTML 文件中
}
?>
生成文件
Page1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page1</title>
</head>
<body>
<h1>Content for Page1</h1></body>