php 批量生成html,txt文件的實(shí)現(xiàn)代碼
更新時(shí)間:2013年06月26日 10:28:56 作者:
本篇文章是對(duì)使用php批量生成html,txt文件的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
首先建立一個(gè)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ù),覆蓋,追加的實(shí)現(xiàn)代碼
- PHP使用內(nèi)置函數(shù)file_put_contents寫入文件及追加內(nèi)容的方法
- PHP中file_put_contents追加和換行的實(shí)現(xiàn)方法
- php刪除txt文件指定行及按行讀取txt文檔數(shù)據(jù)的方法
- php生成txt文件實(shí)例代碼介紹
- php讀取txt文件并將數(shù)據(jù)插入到數(shù)據(jù)庫
- php逐行讀取txt文件寫入數(shù)組的方法
- PHP讀取txt文本文件并分頁顯示的方法
- php中將一段數(shù)據(jù)存到一個(gè)txt文件中并顯示其內(nèi)容
- php生成txt文件標(biāo)題及內(nèi)容的方法
- php讀取txt文件組成SQL并插入數(shù)據(jù)庫的代碼(原創(chuàng)自Zjmainstay)
- php編程實(shí)現(xiàn)追加內(nèi)容到txt文件中的方法
相關(guān)文章
PHP實(shí)現(xiàn)上一篇下一篇的方法實(shí)例總結(jié)
這篇文章主要介紹了PHP實(shí)現(xiàn)上一篇下一篇的方法,結(jié)合實(shí)例形式總結(jié)分析了php獲取上一篇下一篇文章SQL操作的相關(guān)查詢技巧,需要的朋友可以參考下2016-09-09php多數(shù)據(jù)庫支持的應(yīng)用程序設(shè)計(jì)
以前做PHP應(yīng)用,多數(shù)是單數(shù)據(jù)庫數(shù)據(jù)查詢和更新,頂多也是主從數(shù)據(jù)庫的支持,實(shí)現(xiàn)起來相對(duì)簡單。主從數(shù)據(jù)庫的問題在于,當(dāng)會(huì)話存儲(chǔ)在數(shù)據(jù)庫的時(shí)候,同步將可能出現(xiàn)問題,也就是說有可能出現(xiàn)會(huì)話的中斷。2008-08-08php自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按指定key排序的方法
這篇文章主要介紹了php自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按指定key排序的方法,通過自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按照指定鍵值進(jìn)行排序的功能,涉及數(shù)組的遍歷與判定相關(guān)操作技巧,需要的朋友可以參考下2016-09-09解決Yii2郵件發(fā)送結(jié)果返回成功,但接收不到郵件的問題
最近在使用Yii2發(fā)送郵件的時(shí)候遇到了一個(gè)問題,發(fā)送返回提示成功但并沒有收到郵件,所以通過查找相關(guān)的資料,下面這篇文章就來給大家介紹了關(guān)于如何解決Yii2郵件發(fā)送結(jié)果返回成功,但接收不到郵件的問題,需要的朋友可以參考下。2017-05-05linux平臺(tái)編譯安裝PHP7并安裝Redis擴(kuò)展與Swoole擴(kuò)展實(shí)例教程
這篇文章主要介紹了linux平臺(tái)編譯安裝PHP7并安裝Redis擴(kuò)展與Swoole擴(kuò)展的方法,結(jié)合實(shí)例形式詳細(xì)分析了Linux平臺(tái)上安裝php7并安裝Redis擴(kuò)展與Swoole擴(kuò)展的具體步驟與操作技巧,需要的朋友可以參考下2016-09-09在WINDOWS中設(shè)置計(jì)劃任務(wù)執(zhí)行PHP文件的方法
在網(wǎng)上找了些WINDOWS執(zhí)行PHP的計(jì)劃任務(wù)的方法,有一個(gè)寫得很全,可惜在我這竟然沒通過。最后不得不綜合各門派的方法,才能在我這運(yùn)行成功2011-12-12