Home > php教程 > php手册 > body text

php 批量生成html,txt文件的实现代码

WBOY
Release: 2016-06-06 20:29:15
Original
1275 people have browsed it

本篇文章是对使用php批量生成html,txt文件的实现代码进行了详细的分析介绍,需要的朋友参考下

首先建立一个conn.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

复制代码 代码如下:


require_once(“conn.php”);
$query = "SELECT id,title,introduce FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 HTML 结果 */
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"));//读取模板中内容
$str=str_replace("{title}",$title,$str);
$str=str_replace("{introduce}",$introduce,$str);//替换内容
fclose($fp);
$handle=fopen($path,"w"); //写入方式打开新闻路径
fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的HTML文件
fclose($handle);
//echo "生成成功"."
";
}
/* 释放资源 */
mysql_free_result($result);
mysql_close($link);
?>


template.html文件内容:

复制代码 代码如下:






{title}


{introduce}



php 批量生成txt

复制代码 代码如下:


require_once(“conn.php”);
$query = "SELECT kid,title,introduce FROM pro_courses";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 txt 结果 */
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)); //把刚才替换的内容写进生成的txt文件
fclose($handle);
}
/* 释放资源 */
mysql_free_result($result);
mysql_close($link);
?>

,网站空间,香港服务器,香港虚拟主机
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!