php批量生成html,txt文件的实现代码_PHP
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php批量生成html,txt文件的实现代码_PHP,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1891字,纯文字阅读大概需要3分钟。
内容图文
![php批量生成html,txt文件的实现代码_PHP](/upload/InfoBanner/zyjiaocheng/252/0d0e5a729e024b4a95d077396ba7ab45.jpg)
代码如下:
$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);
?>
内容总结
以上是互联网集市为您收集整理的php批量生成html,txt文件的实现代码_PHP全部内容,希望文章能够帮你解决php批量生成html,txt文件的实现代码_PHP所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。