• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-26) 25次浏览 已收录 0个评论

首先建立一个conn.php的文件用来链接数据库

<?php<BR>    $link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error()); <BR>    mysql_query("set names utf8"); <BR>    mysql_select_db("my_database") or die("Could not select database");<BR>?><BR>


php 批量生成html

<?php<BR>    require_once(“conn.php”);<BR>    $query = "SELECT id,title,introduce FROM my_table";<BR>    $result = mysql_query($query) or die("Query failed : " . mysql_error()); <BR>    /* 生成 HTML 结果 */<BR>    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {<br><br>        $id=$row['id'];<BR>        $title=$row['title'];<BR>        $introduce=$row['introduce'];<BR>        $path="html/$id.html";<BR>        $fp=fopen("template.html","r"); //只读打开模板<BR>        $str=fread($fp,filesize("template.html"));//读取模板中内容<BR>        $str=str_replace("{title}",$title,$str);<BR>        $str=str_replace("{introduce}",$introduce,$str);//替换内容<BR>        fclose($fp);<BR>        $handle=fopen($path,"w"); //写入方式打开新闻路径<BR>        fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的HTML文件<BR>        fclose($handle);<BR>        //echo "生成成功"."<br>";<BR>    }<BR>    /* 释放资源 */<BR>    mysql_free_result($result);<BR>    mysql_close($link);<BR>?> <BR>


template.html文件内容:

 <BR><BR><BR><BR><meta http-equiv<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码!网!</b><strong>搞gaodaima代码</strong>="Content-Type" content="text/html; charset=utf-8" /><BR><title>{title}</title><BR><BR><body><BR>{introduce}<BR><BR><BR>


php 批量生成txt

 <BR><?php<BR>    require_once(“conn.php”);<BR>    $query = "SELECT kid,title,introduce FROM pro_courses";<BR>    $result = mysql_query($query) or die("Query failed : " . mysql_error()); <BR>    /* 生成 txt 结果 */<BR>    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {<br><br>        $id=$row['id'];<BR>        $title=$row['title'];<BR>        $introduce=$row['introduce'];<BR>        $path="html/$id.txt";<BR>        $handle=fopen($path,"w"); //写入方式打开新闻路径<BR>        fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的txt文件<BR>        fclose($handle);<BR>    }<BR>    /* 释放资源 */<BR>    mysql_free_result($result);<BR>    mysql_close($link);<BR>?> <BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 批量生成html,txt文件的实现代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址