PHP中批量生成靜態(tài)html(命令行下運(yùn)行PHP)
眾所周知,大部分網(wǎng)站的新聞資訊或商品信息都是靜態(tài)頁(yè)面。這樣做的好處主要是為了:1、加快訪(fǎng)問(wèn)速度,避免過(guò)多的操作數(shù)據(jù)庫(kù);2、SEO優(yōu)化,便于搜索引擎收錄。
本示例圍繞 CMS 系統(tǒng)的靜態(tài)頁(yè)面方案出發(fā),展示批量生成靜態(tài) html 功能。
注:本文程序只能在 Windows 的 DOS 或 Linux 下執(zhí)行 PHP 命令來(lái)運(yùn)行。
本示例主要有4個(gè)文件:config.inc.php(配置文件)、Db.class.php(數(shù)據(jù)庫(kù) PDO 類(lèi))、Model.class.php(PDO數(shù)據(jù)庫(kù)操作類(lèi))、index.php(執(zhí)行文件)
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'); // 數(shù)據(jù)庫(kù)用戶(hù)名
define('DB_PWD', '1715544'); // 數(shù)據(jù)庫(kù)密碼(請(qǐng)您根據(jù)實(shí)際情況自行設(shè)定)
function __autoload($className) {
require_once ROOT_PATH . '/includes/'. ucfirst($className) .'.class.php';
}
?>
Db.class.php
<?php
// 連接數(shù)據(jù)庫(kù)
class Db {
static public function getDB() {
try {
$pdo = new PDO(DB_DSN, DB_USER, DB_PWD);
$pdo->setAttribute(PDO::ATTR_PERSISTENT, true); // 設(shè)置數(shù)據(jù)庫(kù)連接為持久連接
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 設(shè)置拋出錯(cuò)誤
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, true); // 設(shè)置當(dāng)字符串為空轉(zhuǎn)換為 SQL 的 NULL
$pdo->query('SET NAMES utf8'); // 設(shè)置數(shù)據(jù)庫(kù)編碼
} catch (PDOException $e) {
exit('數(shù)據(jù)庫(kù)連接錯(cuò)誤,錯(cuò)誤信息:'. $e->getMessage());
}
return $pdo;
}
}
?>
Model.class.php
<?php
// 操作 SQL
class Model {
/**
* SQL 增刪改操作,返回受影響的行數(shù)
* @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;
}
/**
* 返回全部數(shù)據(jù),返回 PDOStatement 對(duì)象
* @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 號(hào)集合
$idArr = explode(',', $idStr); // 分割成數(shù)組
// 下面的程序循環(huán)生成靜態(tài)頁(yè)面
foreach ($idArr as $articleId) {
$re = $m->getAll("SELECT id,title,date,author,source,content FROM article WHERE id =". $articleId); // $re 為每篇文章的內(nèi)容,注意:其類(lèi)型為:PDOStatement
$article = array(); // $article 為一個(gè)數(shù)組,保存每篇文章的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 為靜態(tài)頁(yè)面放置的目錄
if (!is_dir($articlePath)) mkdir($articlePath, 0777); // 檢查目錄是否存在,不存在則創(chuàng)建
$fileName = ROOT_PATH . '/article/' . $articleId . '.html'; // $fileName 生成的靜態(tài)文件名,格式:文章ID.html(主鍵ID不可能沖突)
$articleTemPath = ROOT_PATH . '/templates/article.html'; // $articleTemPath 文章模板路徑
$articleContent = file_get_contents($articleTemPath); // 獲取模板里面的內(nèi)容
// 對(duì)模板里面設(shè)置的變量進(jìn)行替換。即比如:把模板里面的 <{title}> 替換成數(shù)據(jù)庫(kù)里讀取的 title,替換完畢賦值給變量 $articleContent
$articleContent = getArticle(array_keys($article), $articleContent, $article);
$resource = fopen($fileName, 'w');
file_put_contents($fileName, $articleContent); // 寫(xiě)入 HTML 文件
}
/**
* getArticle($arr, $content, $article) 對(duì)模板進(jìn)行替換操作
* @param array $arr 替換變量數(shù)組
* @param string $content 模板內(nèi)容
* @param array $article 每篇文章內(nèi)容數(shù)組,格式:array('title'=>xx, 'date'=>xx, 'author'=>xx, 'source'=>xx, 'content'=>xx);
*/
function getArticle($arr, $content, $article) {
// 循環(huán)替換
foreach ($arr as $item) {
$content = str_replace('<{'. $item .'}>', $article[$item], $content);
}
return $content;
}
?>
運(yùn)行截圖(Windows 的 DOS 為例)
運(yùn)行完畢截圖:
運(yùn)行2分鐘左右就可以生成 9000多 html。
來(lái)自L(fǎng)ee.的專(zhuān)欄 轉(zhuǎn)載注明出處!??!
相關(guān)文章
PHP使用finfo_file()函數(shù)檢測(cè)上傳圖片類(lèi)型的實(shí)現(xiàn)方法
這篇文章主要介紹了PHP使用finfo_file()函數(shù)檢測(cè)上傳圖片類(lèi)型的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了finfo_file()函數(shù)的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(5) static
php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(5) static...2006-12-12使用PHP實(shí)現(xiàn)Mysql讀寫(xiě)分離
本篇文章是對(duì)使用PHP實(shí)現(xiàn)Mysql讀寫(xiě)分離的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php實(shí)現(xiàn)的遍歷文件夾下所有文件,編輯刪除
遍歷文件夾; 功能:(a)可刪除文件 (b)可編輯文本,網(wǎng)頁(yè)文件 (c)可刪除文件夾,前提是該文件夾為空 (d)可建立文件,文件夾,修改文件夾名稱(chēng)2010-01-01php調(diào)用Google translate_tts api實(shí)現(xiàn)代碼
以下是對(duì)php調(diào)用Google translate_tts api的實(shí)現(xiàn)代碼進(jìn)行了分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08win7+apache+php+mysql環(huán)境配置操作詳解
本篇文章是對(duì)win7+apache+php+mysql環(huán)境配置的操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06