php模板函數(shù) 正則實現(xiàn)代碼
更新時間:2012年10月15日 01:19:59 作者:
有些空閑,就弄了下template函數(shù),比較粗糙。主要是利用正則表達式,把模板文件(html文件)轉換成php文件,從而實現(xiàn)前后臺分離,即是所謂的mvc思想了
我看過phpcms、discuz的源碼,所以可能就缺乏創(chuàng)新了,不過原理大都相通,只是細節(jié)處理可能稍微不同。
說正題,下面開始談談具體實現(xiàn)過程了。
1.首先要想好模板文件放在哪?轉換后的php文件放哪?還有怎么命名?直接上源碼:
function template($tpl = 'index',$dir = 'hello')
{
if(!file_exists($pd = TPL_PATH.$dir.'/'))@mkdir($pd,0777) or die("$pd目錄創(chuàng)建失敗");//如cache/tpl/hello/
if(!file_exists($td = TPL.$dir.'/'))@mkdir($td,0777) or die("$td目錄創(chuàng)建失敗");//如data/tpl/hello/
$t2p = $pd.$tpl.'.php';//模板文件正則轉換后形成的php文件,如cache/tpl/hello/index.php
$t2h = $td.$tpl.'.html';//html模板文件,如data/tpl/hello/index.html
2.什么時候需要正則轉換?可以是正則后的php文件不存在,或正則前的html文件發(fā)生改變時。這里使用到了filemtime(string $path)函數(shù),其返回文件最近修改時間。
if(!file_exists($t2p) || @filemtime($t2p) < @filemtime($t2h) )//模板文件改變后,正則的php文件相應更新
{
template_go($t2p,$t2h);//模板轉換開始
}
return $t2p;//返回正則后的php文件,可以這樣調用:如include template('header','hello');
}
3.開始模板轉換,先從html文件中讀出,然后正則替換,最后寫入php文件中。
function template_go($t2p,$t2h)
{
$str = @file_get_contents($t2h);//讀出
if($str === false) exit("模板文件缺失,請檢查!");
$str = template_do($str);//正則替換
@chmod($t2p,0777);
return $str = file_put_contents($t2p, $str);//寫入
}
4.正則規(guī)則,幾條比較簡略的正則替換語法。
function template_do($str)
{
$str = preg_replace('/([\n\r+])\t+/s', '\\1', $str);//去掉TAB制表符。修正符/s是不忽略換行
$str = preg_replace('/\{\$(.*)\}/Us', '<?php echo $\\1; ?>', $str);/*{$xx}換成<?php echo $xx;?> 注意,必須加上修正符/U,只能匹配一次。也可懶惰匹配*/
$str = preg_replace('/\{php (.+)\}/', '<?php \\1 ?>', $str);/*{php xxxx}換成<?php xxxx ?> 注意,不能加上修正符/s,要考慮多次進行該正則而換行的問題*/
$str = preg_replace('/\{template(.*)\}/Us', '<?php include template\\1; ?>', $str);
/*{template(xx,yy)}換成<?php include template(xx,yy); ?> */
$str = preg_replace('/\{include (.*)\}/Us', '<?php include "\\1"; ?>', $str);/*{include xx.php}換成<?php include xx.php ?> */
$str = "<?php defined('IN_PH') or die('Access Denied');?>".$str;
//$str = preg_replace('/\s+/', ' ', $str);//查看網(wǎng)頁源代碼看看
return $str;
}
當然,這個函數(shù)現(xiàn)在還是比較簡陋的,期待能完善它。
ps:這算是我第一次寫博客,原本是想著有空的話就寫寫技術博客,談談心得,當總結經(jīng)驗教訓了,同時也是向大牛們學習。
還有就是,博客還是比較好保存的,方便省事,呵呵。
說正題,下面開始談談具體實現(xiàn)過程了。
1.首先要想好模板文件放在哪?轉換后的php文件放哪?還有怎么命名?直接上源碼:
復制代碼 代碼如下:
function template($tpl = 'index',$dir = 'hello')
{
if(!file_exists($pd = TPL_PATH.$dir.'/'))@mkdir($pd,0777) or die("$pd目錄創(chuàng)建失敗");//如cache/tpl/hello/
if(!file_exists($td = TPL.$dir.'/'))@mkdir($td,0777) or die("$td目錄創(chuàng)建失敗");//如data/tpl/hello/
$t2p = $pd.$tpl.'.php';//模板文件正則轉換后形成的php文件,如cache/tpl/hello/index.php
$t2h = $td.$tpl.'.html';//html模板文件,如data/tpl/hello/index.html
2.什么時候需要正則轉換?可以是正則后的php文件不存在,或正則前的html文件發(fā)生改變時。這里使用到了filemtime(string $path)函數(shù),其返回文件最近修改時間。
復制代碼 代碼如下:
if(!file_exists($t2p) || @filemtime($t2p) < @filemtime($t2h) )//模板文件改變后,正則的php文件相應更新
{
template_go($t2p,$t2h);//模板轉換開始
}
return $t2p;//返回正則后的php文件,可以這樣調用:如include template('header','hello');
}
3.開始模板轉換,先從html文件中讀出,然后正則替換,最后寫入php文件中。
復制代碼 代碼如下:
function template_go($t2p,$t2h)
{
$str = @file_get_contents($t2h);//讀出
if($str === false) exit("模板文件缺失,請檢查!");
$str = template_do($str);//正則替換
@chmod($t2p,0777);
return $str = file_put_contents($t2p, $str);//寫入
}
4.正則規(guī)則,幾條比較簡略的正則替換語法。
復制代碼 代碼如下:
function template_do($str)
{
$str = preg_replace('/([\n\r+])\t+/s', '\\1', $str);//去掉TAB制表符。修正符/s是不忽略換行
$str = preg_replace('/\{\$(.*)\}/Us', '<?php echo $\\1; ?>', $str);/*{$xx}換成<?php echo $xx;?> 注意,必須加上修正符/U,只能匹配一次。也可懶惰匹配*/
$str = preg_replace('/\{php (.+)\}/', '<?php \\1 ?>', $str);/*{php xxxx}換成<?php xxxx ?> 注意,不能加上修正符/s,要考慮多次進行該正則而換行的問題*/
$str = preg_replace('/\{template(.*)\}/Us', '<?php include template\\1; ?>', $str);
/*{template(xx,yy)}換成<?php include template(xx,yy); ?> */
$str = preg_replace('/\{include (.*)\}/Us', '<?php include "\\1"; ?>', $str);/*{include xx.php}換成<?php include xx.php ?> */
$str = "<?php defined('IN_PH') or die('Access Denied');?>".$str;
//$str = preg_replace('/\s+/', ' ', $str);//查看網(wǎng)頁源代碼看看
return $str;
}
當然,這個函數(shù)現(xiàn)在還是比較簡陋的,期待能完善它。
ps:這算是我第一次寫博客,原本是想著有空的話就寫寫技術博客,談談心得,當總結經(jīng)驗教訓了,同時也是向大牛們學習。
還有就是,博客還是比較好保存的,方便省事,呵呵。
相關文章
php+html5使用FormData對象提交表單及上傳圖片的方法
這篇文章主要介紹了php+html5使用FormData對象提交表單及上傳圖片的方法,實例分析了FormData對象的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-02-02php將圖片保存入mysql數(shù)據(jù)庫失敗的解決方法
這篇文章主要介紹了php將圖片保存入mysql數(shù)據(jù)庫失敗的解決方法,詳細分析了php將圖片保存入數(shù)據(jù)庫遇到的問題及對應的解決方法,并總結了相關的技巧,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12php連接oracle數(shù)據(jù)庫的方法(測試成功)
這篇文章主要介紹了php連接oracle數(shù)據(jù)庫的方法,簡單分析了php連接Oracle數(shù)據(jù)庫的常見方法與具體操作技巧,并對可能出現(xiàn)的問題進行了總結分析,需要的朋友可以參考下2016-05-05PHP實現(xiàn)抓取百度搜索結果頁面【相關搜索詞】并存儲到txt文件示例
這篇文章主要介紹了PHP實現(xiàn)抓取百度搜索結果頁面【相關搜索詞】并存儲到txt文件,涉及php基于curl的頁面抓取及正則匹配相關操作技巧,需要的朋友可以參考下2018-07-07