日期:2014-11-26 分類:網(wǎng)絡技術(shù) 瀏覽:9023 來源:邦明科技
文件一:makehtml.php(PHP生成html靜態(tài)頁面最簡單的原理)
<?php //PHP生成靜態(tài)html頁面原理 $fp = fopen ("template.php","r"); $content = fread ($fp,filesize ("template.php")); $filename="makehtml.html"; $handle = fopen ($filename,"w"); //打開文件指針,創(chuàng)建文件 if (!is_writable ($filename)){ die ("文件:".$filename."不可寫,請檢查其屬性后重試!"); } if (!fwrite ($handle,$content)){ //將信息寫入文件 die ("生成文件".$filename."失敗!"); } fclose ($handle); //關(guān)閉指針 die ("創(chuàng)建文件".$filename."成功!"); ?>
文件二:template.php(PHP模板替換最簡單的原理)
<?php //模板替換原理 $title = '這里是標題'; $hello = '這里是內(nèi)容!'; $file = file_get_contents('template.html'); $file = str_replace(array('{title}','{hello}'),array($title,$hello), $file); echo $file; ?>
文件三:template.html(html模板)
<html> <head> <title>{title}</title> </head> <body> {hello} </body> </html>
文件四:makehtml.html(生成的html頁面)
<?php //模板原理 $title = '這里是標題'; $hello = '這里是內(nèi)容!'; $file = file_get_contents('template.html'); $file = str_replace(array('{title}','{hello}'),array($title,$hello), $file); echo $file; ?>