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

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

 更新時間:2013年06月26日 10:28:56   作者:  
本篇文章是對使用php批量生成html,txt文件的實現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
首先建立一個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)文章

最新評論

曲沃县| 衡阳县| 哈密市| 永嘉县| 梁山县| 平湖市| 黄石市| 象州县| 孙吴县| 江北区| 墨脱县| 瑞昌市| 申扎县| 锦州市| 随州市| 内丘县| 北川| 咸宁市| 琼中| 乌鲁木齐县| 镇赉县| 嫩江县| 宁夏| 乐业县| 桃园县| 青浦区| 井冈山市| 沙洋县| 绵阳市| 仲巴县| 台安县| 宁晋县| 梓潼县| 余姚市| 丹东市| 施秉县| 桂林市| 松潘县| 辽源市| 金山区| 大英县|