国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php 批量生成html,txt文件的實(shí)現(xiàn)代碼

2024-05-04 23:11:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
首先建立一個(gè)conn.php的文件用來(lái)鏈接數(shù)據(jù)庫(kù)

復(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"); //只讀打開(kāi)模板
$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"); //寫(xiě)入方式打開(kāi)新聞路徑
fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫(xiě)進(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"); //寫(xiě)入方式打開(kāi)新聞路徑
fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫(xiě)進(jìn)生成的txt文件
fclose($handle);
}
/* 釋放資源 */
mysql_free_result($result);
mysql_close($link);
?>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金华市| 华坪县| 郯城县| 巧家县| 南丰县| 临沭县| 绥滨县| 晋宁县| 买车| 江北区| 洪洞县| 全南县| 会昌县| 依安县| 密云县| 射阳县| 柘荣县| 四平市| 六枝特区| 鞍山市| 通化县| 娄底市| 清新县| 娄烦县| 大同县| 茌平县| 于都县| 久治县| 通州市| 天等县| 蓬溪县| 阿瓦提县| 华宁县| 宾阳县| 垫江县| 厦门市| 林芝县| 沙河市| 辰溪县| 榆林市| 长汀县|