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

php 批量生成html,txt文件的實(shí)現(xiàn)代碼

首先建立一個(gè)conn.php的文件用來(lái)鏈接數(shù)據(jù)庫(kù)
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?php
    require_once(“conn.php”);
    $query = "SELECT id,title,introduce FROM my_table";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    /* 生成 HTML 結(jié)果 */
    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"); //只讀打開(kāi)模板
        $str=fread($fp,filesize("template.html"));//讀取模板中內(nèi)容
        $str=str_replace("{title}",$title,$str);
        $str=str_replace("{introduce}",$introduce,$str);//替換內(nèi)容
        fclose($fp);
        $handle=fopen($path,"w"); //寫(xiě)入方式打開(kāi)新聞路徑
        fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫(xiě)進(jìn)生成的HTML文件
        fclose($handle);
        //echo "<a href=html/$id.html>生成成功</a>"."<br>";
    }
    /* 釋放資源 */
    mysql_free_result($result);
    mysql_close($link);
?>

template.html文件內(nèi)容:
復(fù)制代碼 代碼如下:
<!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
復(fù)制代碼 代碼如下:
<?php
    require_once(“conn.php”);
    $query = "SELECT kid,title,introduce FROM pro_courses";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    /* 生成 txt 結(jié)果 */
    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"); //寫(xiě)入方式打開(kāi)新聞路徑
        fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫(xiě)進(jìn)生成的txt文件
        fclose($handle);
    }
    /* 釋放資源 */
    mysql_free_result($result);
    mysql_close($link);
?>

php技術(shù)php 批量生成html,txt文件的實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 吴堡县| 青州市| 志丹县| 沅江市| 慈溪市| 五大连池市| 嘉禾县| 忻城县| 香港| 夏邑县| 岫岩| 保德县| 南城县| 张家港市| 盐山县| 合作市| 葫芦岛市| 清苑县| 北碚区| 石台县| 独山县| 古蔺县| 大城县| 社旗县| 萨迦县| 略阳县| 微博| 科技| 岱山县| 清远市| 衡南县| 天峨县| 东兰县| 金门县| 江西省| 敖汉旗| 东兰县| 贡嘎县| 奎屯市| 清苑县| 眉山市|