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

前端之家收集整理的这篇文章主要介绍了php 批量生成html,txt文件的实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先建立一个conn.PHP文件用来链接数据库

代码如下:
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 结果 */
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

代码如下:
PHP
require_once(“conn.PHP”);
$query = "SELECT kid,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,strip_tags($introduce)); //把刚才替换的内容写进生成的txt文件
fclose($handle);
}
/* 释放资源 */
MysqL_free_result($result);
MysqL_close($link);
?>
原文链接:https://www.f2er.com/php/25992.html

猜你在找的PHP相关文章