php 批量生成html,txt文件的实现代码
2018-10-12 14:08
776 查看
首先建立一个conn.php的文件用来链接数据库
php 批量生成html
template.html文件内容:
php 批量生成txt
<?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);
?>
您可能感兴趣的文章:
- PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码
- PHP使用内置函数file_put_contents写入文件及追加内容的方法
- PHP中file_put_contents追加和换行的实现方法
- php删除txt文件指定行及按行读取txt文档数据的方法
- php生成txt文件实例代码介绍
- php读取txt文件并将数据插入到数据库
- php逐行读取txt文件写入数组的方法
- PHP读取txt文本文件并分页显示的方法
- php中将一段数据存到一个txt文件中并显示其内容
- php生成txt文件标题及内容的方法
- php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)
- php编程实现追加内容到txt文件中的方法
相关文章推荐
- php 批量生成html,txt文件的实现代码
- php 批量生成html,txt文件的方法(实例代码)
- php 批量生成html、txt文件
- php 批量生成html、txt文件
- php生成txt文件实例代码介绍
- PHP简单实现生成txt文件到指定目录的方法
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
- php代码生成txt文件并下载
- PHP实现txt文件生成与下载
- PHP生成静态HTML文档实现代码
- windows下PHP批量生成打包android程序APK-渠道txt植入apk文件
- 用PHP ob_start()控制浏览器cache、生成html实现代码
- PHP简单实现生成txt文件到指定目录的方法
- php生成txt文件实例代码介绍
- PHP实现HTML生成PDF文件的方法
- php添加文章时生成静态HTML文章的实现代码
- PHP实现HTML生成PDF文件的方法
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
- PHP定时自动生成静态HTML的实现代码