溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用php怎么批量生成html和txt文件

發布時間:2021-02-05 18:05:18 來源:億速云 閱讀:230 作者:Leah 欄目:開發技術

這篇文章給大家介紹使用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怎么批量生成html和txt文件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女