最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2013年06月26日 10:28:56   作者:  
本篇文章是對(duì)使用php批量生成html,txt文件的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
首先建立一個(gè)conn.php的文件用來鏈接數(shù)據(jù)庫
復(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"); //只讀打開模板
        $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"); //寫入方式打開新聞路徑
        fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫進(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"); //寫入方式打開新聞路徑
        fwrite($handle,strip_tags($introduce)); //把剛才替換的內(nèi)容寫進(jìn)生成的txt文件
        fclose($handle);
    }
    /* 釋放資源 */
    mysql_free_result($result);
    mysql_close($link);
?>

相關(guān)文章

最新評(píng)論

景泰县| 察雅县| 黔江区| 淄博市| 黑水县| 汉源县| 邛崃市| 滕州市| 兰溪市| 勃利县| 兴国县| 牡丹江市| 垫江县| 大英县| 达孜县| 犍为县| 安阳县| 凤台县| 榆树市| 青海省| 洮南市| 锦屏县| 宿迁市| 紫云| 中宁县| 都昌县| 贺州市| 苏州市| 红原县| 河间市| 扶绥县| 务川| 马鞍山市| 托克逊县| 阿坝县| 苍南县| 疏勒县| 新干县| 甘洛县| 甘南县| 隆回县|