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

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

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

复制代码 代码如下: <?php $link = mysql_connect("mysql_host" "mysql_user" "mysql_password" )or die("Could not connect : " mysql_error())mysql_query("set names utf ")mysql_select_db("my_database") or die("Could not select database")?>

php 批量生成

复制代码 代码如下: <?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="/$id "$fp=fopen("template " "r")//只读打开模板 $str=fread($fp filesize("template "))//读取模板中内容 $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=/$id >生成成功</a>" "<br>"} /* 释放资源 */ mysql_free_result($result)mysql_close($link)?>

template 文件内容

复制代码 代码如下: <!DOCTYPE PUBLIC " //W C//DTD XHTML Transitional//EN" " <xmlns=" <head><meta equiv="Content Type" content="text/charset=utf " /><title>{title}</title></head><body>{introduce} </body></>

php 批量生成txt

复制代码 代码如下: lishixinzhi/Article/program/PHP/201311/20863

<?php

 $Html = '<html><head><title>这是一个html文档</title></head><body>这是php生成的一个html文档</body></html>'

 file_put_contents( 'abc.html', $Html )

上面代码运行后,就会生成一个abc.html文件

自己根据这个代码进行研究下!

修改如下:

function ggg($aaa,$bbb){

return $aaa."\n". $bbb . "\n"

}

$Html = ggg(11,22)

file_put_contents("test/1.html", $Html)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6103245.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存