首先建立一个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}</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 批量生成html、txt文件的更多相关文章

  1. php 批量生成html,txt文件的方法(实例代码)

    php批量生成html,txt文件的实现代码. 首先,建立一个conn.php 链接数据库. <?php $link = mysql_connect("mysql_host" ...

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

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

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

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

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

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

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

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

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

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

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

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

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

    主要使用目的: 任何应用程序通常需要设置安装所需并依赖一组类库来满足工作要求.通过requirements.txt可以一次性安装程序所需要和依赖的包. 为工程生成requirements.txt的两种 ...

  9. Python - 生成 requirement.txt 文件

    前言 Python项目中,一般都会有一个 requirements.txt 文件 这个文件主要是用于记录当前项目下的所有依赖包及其精确的版本号,以方便在一个新环境下更快的进行部署 如何生成 requi ...

  10. Python项目生成requirements.txt文件之pipreqs的使用

    生成requirements.txt时使用pip freeze > requirements.txt会将环境下所有的安装包都进行生成,再进行安装的时候会全部安装很多没有的包.耗时耗力其实是不可取 ...

随机推荐

  1. Usermod 命令详解 ------工作中修改shell时用 usermod -s /bin/csh home

     Usermod 命令详解 2012-09-11 11:01:36 标签:usermod 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.htt ...

  2. 一个C++类的注释:

    #ifndef __RUNTIMEPARA__HPP#define __RUNTIMEPARA__HPP #include <string> //后面会有介绍 #include <m ...

  3. JSON 与 String、Map、JavaBean互转

    JSON 与 String.Map.JavaBean互转 //解析远程登录用户信息 AttributePrincipal principal = AssertionHolder.getAssertio ...

  4. POJ 1054 The Troublesome Frog

    The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 9581 Accepted: 2883 ...

  5. nginx(三)初步搭建nginx虚拟主机

    上面就是nginx基于域名.ip访问的配置,掌握住格式,就很好配置了. 一.基于域名的虚拟主机的配置:1.我们在此复习一下DNS的配置:[root@mgmserver /]# hostnamemgms ...

  6. django-cms 代码研究(七)杂七杂八

    实体关系图 核心对象: cms_page/cms_placeholder/cms_cmsplugin. page模型类继承关系图 CMSPlugin&Placeholder模型类继承关系图 = ...

  7. 【云计算】Netflix 开源持续交付平台 Spinnaker

    oschina        发布于: 2015年11月19日 (0评)          分享到:    收藏 +1 CDS首都在线全球云主机.全球私有网络,开工送礼,免费试用! »   日前,Ne ...

  8. Java 7 的7个新特性

    1.对集合类的语言支持:(??) 2.自动资源管理: 3.改进的通用实例创建类型推断:(??) 4.数字字面量下划线支持:(√) 5.switch中使用string:(√) 6.二进制字面量:(√) ...

  9. Convert Sorted Array to Binary Search Tree With Minimal Height

    Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...

  10. 73 [面试题]交换一个整数的二进制表示的奇偶位(swapOddEvenBits)

    [本文链接] http://www.cnblogs.com/hellogiser/p/swap-odd-even-bits.html [分析] 假定一个数字是8位数,设为ABCDEFGH ABCDEF ...