色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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}</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 批量生成html,txt文件的實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 瑞安市| 林州市| 子长县| 新巴尔虎右旗| 顺昌县| 西畴县| 方正县| 霍邱县| 富民县| 肇庆市| 武义县| 观塘区| 广水市| 岢岚县| 海晏县| 亚东县| 综艺| 九江市| 新乡县| 平果县| 子洲县| 贺兰县| 福贡县| 克什克腾旗| 深州市| 务川| 嘉黎县| 太和县| 绩溪县| 余姚市| 夏河县| 胶州市| 鄯善县| 宿松县| 定西市| 利津县| 谷城县| 平武县| 延长县| 武宣县| 廉江市|