Home > Backend Development > PHP Tutorial > PHP batch generates html and txt files

PHP batch generates html and txt files

WBOY
Release: 2016-07-25 08:54:15
Original
1414 people have browsed it
  1. $link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error());
  2. mysql_query("set names utf8");
  3. mysql_select_db("my_database") or die("Could not select database");
  4. ?>
复制代码

php 批量生成html

  1. require_once(“conn.php”);

  2. $query = "SELECT id,title,introduce FROM my_table";

  3. $result = mysql_query($query) or die("Query failed : " . mysql_error());

  4. /* 生成 HTML 结果 */

  5. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  6. $id=$row['id'];
  7. $title=$row['title'];
  8. $introduce=$row['introduce'];
  9. $path="html/$id.html";
  10. $fp=fopen("template.html","r"); //只读打开模板
  11. $str=fread($fp,filesize("template.html"));//读取模板中内容
  12. $str=str_replace("{title}",$title,$str);
  13. $str=str_replace("{introduce}",$introduce,$str);//替换内容
  14. fclose($fp);
  15. $handle=fopen($path,"w"); //写入方式打开新闻路径
  16. fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的HTML文件
  17. fclose($handle);
  18. //echo "生成成功"."
    ";
  19. }

  20. /* 释放资源 */

  21. mysql_free_result($result);
  22. mysql_close($link);
  23. ?>

复制代码

template.html文件内容:

  1. {title}
  2. {introduce}
复制代码

php 批量生成txt

  1. require_once(“conn.php”);

  2. $query = "SELECT kid,title,introduce FROM pro_courses";

  3. $result = mysql_query($query) or die("Query failed : " . mysql_error());

  4. /* 生成 txt 结果 */

  5. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  6. $id=$row['id'];
  7. $title=$row['title'];
  8. $introduce=$row['introduce'];
  9. $path="html/$id.txt";
  10. $handle=fopen($path,"w"); //写入方式打开新闻路径
  11. fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的txt文件
  12. fclose($handle);
  13. }

  14. /* 释放资源 */

  15. mysql_free_result($result);
  16. mysql_close($link);
  17. ?>

复制代码


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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template