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);
?>
php 批量生成html,txt文件的方法(实例代码)的更多相关文章
- pipreqs 生成requirements.txt文件时编码错误问题
1,首先安装pipreqs --> pip install pipreqs 2.生成相应项目的路径 --> pipreqs e:\a\b 在此时可能会遇见 UnicodeDecodeE ...
- Javascript写入txt和读取txt文件的方法
文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...
- shell下批量重命名svn文件的方法
shell下批量重命名svn文件的方法 目标: 将svn目录下所有文件重命名 , 原文件前缀为 ucc_ , 批量改为 xmd_ 用tree看下当前svn目录 ucc_1.c ucc_1.h ucc_ ...
- 将眼底图片生成的txt文件进行格式化处理
# -*- coding: utf-8 -*- """ 将图片转换生成的txt文件进行格式化处理 """ import os import ...
- python 项目自动生成 requirements.txt 文件
生成 requirements.txt 文件的目的: 安装 pthon 项目时需要把此项目所有依赖的第三方包安装完成.项目依赖的第三方包统一放到 requirements.txt 文件中即可. 怎么自 ...
- 导出OpenID为txt文件的方法
导出OpenID为txt文件的方法 public function export(){ $shop = M("Shop"); $arr = $shop->field('ope ...
- ANR无法生成traces.txt文件
在有些设备上ANR无法生成traces.txt文件,kill -3又不能准确把握时机或者没权限,可能是软件访问这个路径失败,可以在/data/anr/下面建立一个空的traces.txt,注意保证an ...
- Python中如何生成requirements.txt文件
Python项目中一般都包含一个名为 requirements.txt 文件,用来记录当前项目所有的依赖包和版本号,在一个新环境下通过该文件可以更方便的构建项目所需要的运行环境. 生成requirem ...
- Python项目生成requirements.txt文件及pip升级问题解决及流程
缘由:新项目使用Python, PC上的python包不全,需要通过requirements.txt文件指定安装所需包 pip安装遇到一些坑 一.直接使用pip包管理工具生成requirements. ...
随机推荐
- restful php
http://bbs.phpchina.com/thread-228725-1-1.html http://www.cnblogs.com/artech/p/3506553.html http://w ...
- bash的for循环从命令读取值
bash的for循环可以很方便地从命令读取值,还可以指定分割值 下面的程序可以打印文件的内容,前面加上行号 #!/bin/bash # 打印每一行的内容,前面加行号 filename="/h ...
- JavaScript 之 弹出窗口总结
一.javascript 控制窗口关闭及刷新 //关闭弹窗 <script language="javascript"> window.close(); </sc ...
- MySQL(19):SQL语句(MySQL)大全
SQL语句大全一.创建和删除数据库 1.创建用户 //创建用户且置密码,在MySQL中行,但在Oracle中行 ----必须在超级管理员身份下操作 create user hncu identifi ...
- javaweb学习总结十二(JAXP对XML文档进行SAX解析)
一:JAXP使用SAX方式解析XML文件 1:dom解析与sax解析异同点 2:sax解析特点 二:代码案例 1:xml文件 <?xml version="1.0" enco ...
- poj 2186 强连通分支 和 spfa
思路: 建图时,分别建正向图edge和转置图T.用正向图edge来DFS,找出第一个被发现的强连通分支(如果该图存在题目要求的点,那么一定就是第一个被发现的).然后用spfa跑转置图T,判断被发现的点 ...
- AutoHotKey 脚本集中营(一)
前言:实战是最好的学习.本集中营共分 x+mc2 期(x 为已发行的期数,m 为我的空闲时间,c 为我的更新动力),旨在通过对脚本代码的阅读而提高 AHK 语言的熟练程度.脚本的难度与期数成正比,有兴 ...
- Delphi各个版本和发展历史(转)
Delphi,是Windows平台下著名的快速应用程序开发工具(Rapid Application Development,简称RAD).它的前身,即是DOS时代盛行一时的“BorlandTurbo ...
- MVC清除缓存设置+数据验证
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)] [Table("User")]:定义UserD ...
- hihocoder 1038 01背包
#1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...