PHP之靜態HTML

靜態HTML和從數據庫裏讀取內容的動態頁相比一直都有其不可替換的良好表現。在空間不做爲第一考慮因素的時候,靜態HTML顯示更加適用。

PHP生成靜態頁,我總結了下有以下兩個方法:

<?php
$src = './index.tpl';

$content = file_get_content($src);
$content = str_replace('{title}' , '標題' , $content);
//相同替換
$content = str_replace( ... );

$fp = fopen('./index.html' , 'w') or die('can not open file');
fputs($fp , $content);
fclose($fp);
unset($fp);

index.tpl

<div id='title'>{title}</div>

 

第二兩種就相對簡單多了

<?php
ob_start();

$top_id = 34;
require './index.php';

ob_end_clean();

/**
 *在index.php 可以將$top_id做爲參數;因爲這個是可以傳遞到index.php這個頁面的。
 *然後在index.php裏寫入生成HTML的代碼。即不需要替換也可以生成HTML;
 */


 

發佈了48 篇原創文章 · 獲贊 16 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章