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文件的方法(实例代码)的更多相关文章

  1. pipreqs 生成requirements.txt文件时编码错误问题

    1,首先安装pipreqs --> pip install pipreqs 2.生成相应项目的路径  --> pipreqs  e:\a\b 在此时可能会遇见 UnicodeDecodeE ...

  2. Javascript写入txt和读取txt文件的方法

    文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...

  3. shell下批量重命名svn文件的方法

    shell下批量重命名svn文件的方法 目标: 将svn目录下所有文件重命名 , 原文件前缀为 ucc_ , 批量改为 xmd_ 用tree看下当前svn目录 ucc_1.c ucc_1.h ucc_ ...

  4. 将眼底图片生成的txt文件进行格式化处理

    # -*- coding: utf-8 -*- """ 将图片转换生成的txt文件进行格式化处理 """ import os import ...

  5. python 项目自动生成 requirements.txt 文件

    生成 requirements.txt 文件的目的: 安装 pthon 项目时需要把此项目所有依赖的第三方包安装完成.项目依赖的第三方包统一放到 requirements.txt 文件中即可. 怎么自 ...

  6. 导出OpenID为txt文件的方法

    导出OpenID为txt文件的方法 public function export(){ $shop = M("Shop"); $arr = $shop->field('ope ...

  7. ANR无法生成traces.txt文件

    在有些设备上ANR无法生成traces.txt文件,kill -3又不能准确把握时机或者没权限,可能是软件访问这个路径失败,可以在/data/anr/下面建立一个空的traces.txt,注意保证an ...

  8. Python中如何生成requirements.txt文件

    Python项目中一般都包含一个名为 requirements.txt 文件,用来记录当前项目所有的依赖包和版本号,在一个新环境下通过该文件可以更方便的构建项目所需要的运行环境. 生成requirem ...

  9. Python项目生成requirements.txt文件及pip升级问题解决及流程

    缘由:新项目使用Python, PC上的python包不全,需要通过requirements.txt文件指定安装所需包 pip安装遇到一些坑 一.直接使用pip包管理工具生成requirements. ...

随机推荐

  1. AngularJS - Apply方法监听model变化

    <body> <div ng-app="myApp"> <div ng-controller="firstController" ...

  2. SQL Insert语句数据以以unicode码存储 解决存储数据出现乱码的问题

    写了个读取原始的文本数据导入数据库的工具 ,最后发现空中有几个值是乱码 例如 原始数据是 :Bjørn 存到数据库中是 Bj?rn 研究半天发现是一直以来忽略了一个标记‘N’ 2条 Insert 语句 ...

  3. Getting Started with Zend Framework MVC Applications

    Getting Started with Zend Framework MVC Applications This tutorial is intended to give an introducti ...

  4. cocos2d-x动作原理

    首先CCAction是所有动作的基类,如下图继承关系: 那么来看看CCAction的定义: class CC_DLL CCAction : public CCObject { public: CCAc ...

  5. uva 699 The Falling Leaves dfs实现

    额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...

  6. CSS: Float a div on another div, Ex: Text caption on picture

    <style type="text/css"> .figure { width: 316px; height: 205px; display: block; borde ...

  7. Wing IDE编译TesorFlow中Mnist convolutional 实例

    # # http://www.cnblogs.com/mydebug/ # from __future__ import absolute_import from __future__ import ...

  8. javascript权威指南(中文版)中的一些错误(一)

    本人目前正在学习js,使用的是javascript权威指南(中文版),学习的时候发现一些细节上的错误,若是我的错误,欢迎指正 1.P11------多了“我们称为 原文为 return Math.sq ...

  9. [转]HTML accesskey 属性

    本文转自:http://www.dreamdu.com/xhtml/attribute_accesskey/ 17.2. HTML accesskey 属性 accesskey 属性  --  代表链 ...

  10. Quartz Scheduler(2.2.1) - Usage of CronTriggers

    Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerfu ...