php 批量生成html,txt文件的實現(xiàn)代碼
更新時間:2013年06月26日 10:28:56 作者:
本篇文章是對使用php批量生成html,txt文件的實現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
首先建立一個conn.php的文件用來鏈接數(shù)據(jù)庫
<?php
$link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error());
mysql_query("set names utf8");
mysql_select_db("my_database") or die("Could not select database");
?>
php 批量生成html
<?php
require_once(“conn.php”);
$query = "SELECT id,title,introduce FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 HTML 結(jié)果 */
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id=$row['id'];
$title=$row['title'];
$introduce=$row['introduce'];
$path="html/$id.html";
$fp=fopen("template.html","r"); //只讀打開模板
$str=fread($fp,filesize("template.html"));//讀取模板中內(nèi)容
$str=str_replace("{title}",$title,$str);
$str=str_replace("{introduce}",$introduce,$str);//替換內(nèi)容
fclose($fp);
$handle=fopen($path,"w"); //寫入方式打開新聞路徑
fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫進(jìn)生成的HTML文件
fclose($handle);
//echo "<a href=html/$id.html>生成成功</a>"."<br>";
}
/* 釋放資源 */
mysql_free_result($result);
mysql_close($link);
?>
template.html文件內(nèi)容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title}</title>
</head>
<body>
{introduce}
</body>
</html>
php 批量生成txt
<?php
require_once(“conn.php”);
$query = "SELECT kid,title,introduce FROM pro_courses";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 txt 結(jié)果 */
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id=$row['id'];
$title=$row['title'];
$introduce=$row['introduce'];
$path="html/$id.txt";
$handle=fopen($path,"w"); //寫入方式打開新聞路徑
fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫進(jìn)生成的txt文件
fclose($handle);
}
/* 釋放資源 */
mysql_free_result($result);
mysql_close($link);
?>
復(fù)制代碼 代碼如下:
<?php
$link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error());
mysql_query("set names utf8");
mysql_select_db("my_database") or die("Could not select database");
?>
php 批量生成html
復(fù)制代碼 代碼如下:
<?php
require_once(“conn.php”);
$query = "SELECT id,title,introduce FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 HTML 結(jié)果 */
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id=$row['id'];
$title=$row['title'];
$introduce=$row['introduce'];
$path="html/$id.html";
$fp=fopen("template.html","r"); //只讀打開模板
$str=fread($fp,filesize("template.html"));//讀取模板中內(nèi)容
$str=str_replace("{title}",$title,$str);
$str=str_replace("{introduce}",$introduce,$str);//替換內(nèi)容
fclose($fp);
$handle=fopen($path,"w"); //寫入方式打開新聞路徑
fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫進(jìn)生成的HTML文件
fclose($handle);
//echo "<a href=html/$id.html>生成成功</a>"."<br>";
}
/* 釋放資源 */
mysql_free_result($result);
mysql_close($link);
?>
template.html文件內(nèi)容:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title}</title>
</head>
<body>
{introduce}
</body>
</html>
php 批量生成txt
復(fù)制代碼 代碼如下:
<?php
require_once(“conn.php”);
$query = "SELECT kid,title,introduce FROM pro_courses";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 txt 結(jié)果 */
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id=$row['id'];
$title=$row['title'];
$introduce=$row['introduce'];
$path="html/$id.txt";
$handle=fopen($path,"w"); //寫入方式打開新聞路徑
fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫進(jìn)生成的txt文件
fclose($handle);
}
/* 釋放資源 */
mysql_free_result($result);
mysql_close($link);
?>
您可能感興趣的文章:
- PHP創(chuàng)建文件,并向文件中寫入數(shù)據(jù),覆蓋,追加的實現(xiàn)代碼
- PHP使用內(nèi)置函數(shù)file_put_contents寫入文件及追加內(nèi)容的方法
- PHP中file_put_contents追加和換行的實現(xiàn)方法
- php刪除txt文件指定行及按行讀取txt文檔數(shù)據(jù)的方法
- php生成txt文件實例代碼介紹
- php讀取txt文件并將數(shù)據(jù)插入到數(shù)據(jù)庫
- php逐行讀取txt文件寫入數(shù)組的方法
- PHP讀取txt文本文件并分頁顯示的方法
- php中將一段數(shù)據(jù)存到一個txt文件中并顯示其內(nèi)容
- php生成txt文件標(biāo)題及內(nèi)容的方法
- php讀取txt文件組成SQL并插入數(shù)據(jù)庫的代碼(原創(chuàng)自Zjmainstay)
- php編程實現(xiàn)追加內(nèi)容到txt文件中的方法
相關(guān)文章
golang與php實現(xiàn)計算兩個經(jīng)緯度之間距離的方法
這篇文章主要介紹了golang與php實現(xiàn)計算兩個經(jīng)緯度之間距離的方法,結(jié)合實例形式對比分析了Go語言與php進(jìn)行經(jīng)緯度計算的相關(guān)數(shù)學(xué)運算技巧,需要的朋友可以參考下2016-07-07
將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼
本節(jié)主要介紹了將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼,需要的朋友可以參考下2014-07-07
PHP實現(xiàn)對文本數(shù)據(jù)庫的常用操作方法實例演示
這篇文章主要介紹了PHP實現(xiàn)對文本數(shù)據(jù)庫的常用操作方法,需要的朋友可以參考下2014-07-07
js+php實現(xiàn)靜態(tài)頁面實時調(diào)用用戶登陸狀態(tài)的方法
這篇文章主要介紹了js+php實現(xiàn)靜態(tài)頁面實時調(diào)用用戶登陸狀態(tài)的方法,采用在靜態(tài)頁面中使用js調(diào)用php頁面從而實現(xiàn)用戶登錄狀態(tài)的實時調(diào)用功能,需要的朋友可以參考下2015-01-01
php構(gòu)造函數(shù)與析構(gòu)函數(shù)
PHP析構(gòu)函數(shù)與構(gòu)造函數(shù)是相反的,它們被調(diào)用來一個對象從內(nèi)存中銷毀,幫助我們釋放對象屬性所占用的內(nèi)存并銷毀對象相關(guān)的資源。2016-04-04
PHP 應(yīng)用程序的安全 -- 不能違反的四條安全規(guī)則
PHP 應(yīng)用程序的安全 -- 不能違反的四條安全規(guī)則...2006-11-11
在PHP中實現(xiàn)Javascript的escape()函數(shù)代碼
在 Javascript 中,可以利用 escape/unescape() 和 eval_r() 函數(shù)進(jìn)行簡單的轉(zhuǎn)碼處理,讓普通的 URL 網(wǎng)址看起來比較怪異,使那些令人討厭的搜索爬蟲無法辨認(rèn)你希望隱藏的資源。2010-08-08

