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);
?>

  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓   ┃                                  ┃   ┃ 点击“生成“按钮后,所生成的文本自动放入剪贴板,可直接粘贴使用。 ┃   ┃                                  ┃   ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┏━━━┓ ┃举例:┃ ┗━━━┛ 【先按如下设置】  前缀内容: <IMG SRC="http://www.LinTongJY.com/images/Pic-200611  中缀开始: 1   中缀结束: 10   中缀位数 3  后缀内容: -JDCZ.jpg"><br>   换行  启用分节  每 3 次为一节  节尾添加: <HR><!-- 每三张图片插入一条水平线 -->   换行 【可生成以下文本】 <IMG SRC="http://www.LinTongJY.com/images/Pic-200611001-JDCZ.jpg"><br> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611002-JDCZ.jpg"><br> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611003-JDCZ.jpg"><br> <HR><!-- 每三张图片插入一条水平线 --> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611004-JDCZ.jpg"><br> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611005-JDCZ.jpg"><br> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611006-JDCZ.jpg"><br> <HR><!-- 每三张图片插入一条水平线 --> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611007-JDCZ.jpg"><br> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611008-JDCZ.jpg"><br> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611009-JDCZ.jpg"><br> <HR><!-- 每三张图片插入一条水平线 --> <IMG SRC="http://www.LinTongJY.com/images/Pic-200611010-JDCZ.jpg"><br>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值