欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中批量生成静态html(命令行下运行PHP)

程序员文章站 2023-11-20 16:25:40
众所周知,大部分网站的新闻资讯或商品信息都是静态页面。这样做的好处主要是为了:1、加快访问速度,避免过多的操作数据库;2、seo优化,便于搜索引擎收录。 本示例围绕 cm...

众所周知,大部分网站的新闻资讯或商品信息都是静态页面。这样做的好处主要是为了:1、加快访问速度,避免过多的操作数据库;2、seo优化,便于搜索引擎收录。

本示例围绕 cms 系统的静态页面方案出发,展示批量生成静态 html 功能。
注:本文程序只能在 windows 的 dos 或 linux 下执行 php 命令来运行。
本示例主要有4个文件:config.inc.php(配置文件)、db.class.php(数据库 pdo 类)、model.class.php(pdo数据库操作类)、index.php(执行文件)
config.inc.php

复制代码 代码如下:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('prc');
define('root_path', dirname(__file__)); // 根目录
define('db_dsn', 'mysql:host=localhost;dbname=article'); // mysql 的 pdo dsn
define('db_user', 'root'); // 数据库用户名
define('db_pwd', '1715544'); // 数据库密码(请您根据实际情况自行设定)
function __autoload($classname) {
 require_once root_path . '/includes/'. ucfirst($classname) .'.class.php';
}
?>

db.class.php

复制代码 代码如下:

<?php
// 连接数据库
class db {
 static public function getdb() {
  try {
   $pdo = new pdo(db_dsn, db_user, db_pwd);
   $pdo->setattribute(pdo::attr_persistent, true); // 设置数据库连接为持久连接
   $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);  // 设置抛出错误
   $pdo->setattribute(pdo::attr_oracle_nulls, true);  // 设置当字符串为空转换为 sql 的 null
   $pdo->query('set names utf8');  // 设置数据库编码
  } catch (pdoexception $e) {
   exit('数据库连接错误,错误信息:'. $e->getmessage());
  }
  return $pdo;
 }
}
?>

model.class.php

复制代码 代码如下:

<?php
// 操作 sql
class model {
 /**
  * sql 增删改操作,返回受影响的行数
  * @param string $sql
  * @return int
  */
 public function aud($sql) {
  try {
   $pdo = db::getdb();
   $row = $pdo->exec($sql);
  } catch (pdoexception $e) {
   exit($e->getmessage());
  }
  return $row;
 }

 /**
  * 返回全部数据,返回 pdostatement 对象
  * @param string $sql
  * @return pdostatement
  */
 public function getall($sql) {
  try {
   $pdo = db::getdb();
   $result = $pdo->query($sql);
   return $result;
  } catch (pdoexception $e) {
   exit($e->getmessage());
  }
 }
}
?>

index.php

复制代码 代码如下:

<?php
require_once './config.inc.php';
$m = new model();
$ids = $m->getall("select id from article order by id asc");
foreach ($ids as $rowidarr) {
 $idstr .= $rowidarr['id'].',';
}
$idstr = rtrim($idstr, ','); // 所有文章的 id 号集合
$idarr = explode(',', $idstr); // 分割成数组
// 下面的程序循环生成静态页面
foreach ($idarr as $articleid) {
 $re = $m->getall("select id,title,date,author,source,content from article where id =". $articleid); // $re 为每篇文章的内容,注意:其类型为:pdostatement
 $article = array(); // $article 为一个数组,保存每篇文章的title、date、author、content、source
 foreach ($re as $r) {
  $article = array(
     'title'=>$r['title'],
     'date'=>$r['date'],
     'author'=>$r['author'],
     'source'=>$r['source'],
     'content'=>$r['content']
    );
 }
 $articlepath = root_path. '/article'; // $articlepath 为静态页面放置的目录
 if (!is_dir($articlepath)) mkdir($articlepath, 0777); // 检查目录是否存在,不存在则创建
 $filename = root_path . '/article/' . $articleid . '.html'; // $filename 生成的静态文件名,格式:文章id.html(主键id不可能冲突)
 $articletempath = root_path . '/templates/article.html'; // $articletempath 文章模板路径
 $articlecontent = file_get_contents($articletempath); // 获取模板里面的内容
 // 对模板里面设置的变量进行替换。即比如:把模板里面的 <{title}> 替换成数据库里读取的 title,替换完毕赋值给变量 $articlecontent
 $articlecontent = getarticle(array_keys($article), $articlecontent, $article);
 $resource = fopen($filename, 'w');
 file_put_contents($filename, $articlecontent); // 写入 html 文件
}

/**
 * getarticle($arr, $content, $article) 对模板进行替换操作
 * @param array $arr 替换变量数组
 * @param string $content 模板内容
 * @param array $article 每篇文章内容数组,格式:array('title'=>xx, 'date'=>xx, 'author'=>xx, 'source'=>xx, 'content'=>xx);
 */
function getarticle($arr, $content, $article) {
 // 循环替换
 foreach ($arr as $item) {
  $content = str_replace('<{'. $item .'}>', $article[$item], $content);
 }
 return $content;
}
?>

 运行截图(windows 的 dos 为例) 
PHP中批量生成静态html(命令行下运行PHP)       
运行完毕截图: 

PHP中批量生成静态html(命令行下运行PHP)

运行2分钟左右就可以生成 9000多 html。

来自lee.的专栏 转载注明出处!!!