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. Step-by-Step XML Free Spring MVC 3 Configuration--reference

    The release of Spring 2.5 reduce the burden of XML by introduction annotation based configuration, b ...

  2. 美国L1签证和B1,E2签证的区别

    L1是跨国公司派驻工作人员到美国关联公司工作所需的签证.L1有两种: L1A是给管理人员的.L1B是给关键技术人员的.通过延期,L1A最长时间可达7年. L1B最长时间可达五年. 最初的L1申请,如果 ...

  3. mysql表名大小写问题

               默认情况下,MySQL 将以小写保存表名.一个避免 MySQL 服务器小写问题方法是以 -O lower_case_table_names=0 启动 mysqld.默认情况下,这 ...

  4. MySQL(19):SQL语句(MySQL)大全

    SQL语句大全一.创建和删除数据库 1.创建用户 //创建用户且置密码,在MySQL中行,但在Oracle中行  ----必须在超级管理员身份下操作 create user hncu identifi ...

  5. request 和response 中的setCharacterEncoding区别

    response和request的setCharacterEncoding 一.request.setCharacterEncoding():是设置从request中取得的值或从数据库中取出的值. 指 ...

  6. fancybox的使用

    fancybox,个人没有深入了解,只是为了工作需要,做的一些界面,主要是用的AJAX功能. 首先,需要下载fancybox的js文件以及CSS文件(可能用不到) 其次,在页面中引入 <scri ...

  7. Cisco 交换机的操作

    Cisco的工作模式 Cisco设备有常用模式为:用户模式.特权模式.全局模式.端口模式.首先它们之间呈现出递进关系:用户模式->特权模式->全局模式->端口模式 1.用户模式 交换 ...

  8. 钓鱼 贪心 end

    #include<iostream> int m,n; int *p; int dis=0; int peo=0; int data[3][2]; int b[3][2]; int da[ ...

  9. Win10开机小键盘不亮解决办法

    1.首先修改注册表打开[HKEY_USERS\.DEFAULT\Control Panel\Keyboard]项,将"InitialKeyboardIndicators"的键值从& ...

  10. Linux 命令 - uniq: 通知或忽略重复行

    给定一个已排好序的文件,uniq 会删除重复行并将结果输出到标准输出中.uniq 通常与 sort 结合使用以删除 sort 输出内容中的重复行. 命令格式 uniq [OPTION]... [INP ...