您当前的位置: 首页 > 网站编程 > PHP教程 > php 批量生成html,txt文件的实现代码

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

作者:不详 来源:网络 发布时间: 2014-08-19 09:16 点击:
首先建立一个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

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 "<a href=html/$id.html>生成成功</a>"."<br>";

  }

  /* 释放资源 */

  mysql_free_result($result);

  mysql_close($link);

  ?>

  template.html文件内容:

  


  复制代码 代码如下:

  <!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 结果 */

  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文件的实现代码"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4