zoukankan      html  css  js  c++  java
  • 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 "<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}-www.jbxue.com</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);
    } //by www.jbxue.com
    /* 释放资源 */
    mysql_free_result($result);
    mysql_close($link);
    ?>
  • 相关阅读:
    poj 1984 Navigation Nightmare(带权并查集+小小的技巧)
    zoj 3261 Connections in Galaxy War(并查集逆向加边)
    poj 1733 Parity game(带权并查集)
    poj 1456 Supermarket(贪心+优先队列)
    hdu 3038 How Many Answers Are Wrong(并查集的思想利用)
    poj 1182 食物链(种类并查集 ‘初心者’)
    hdu 1182 A Bug's Life(简单种类并查集)
    hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)
    CodeForces 779D. String Game(二分答案)
    poj 3169 Layout(差分约束+spfa)
  • 原文地址:https://www.cnblogs.com/study100/p/3216289.html
Copyright © 2011-2022 走看看