程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> phplib類中文詳解

phplib類中文詳解

編輯:PHP綜合
<?php
/*
* PHPlib模板7.4中文版(不足之處還請各位指正)
* (C) Copyright 1999-2000 NetUSE GmbH
* Kristian Koehntopp
* 彭贊群注釋於2004年6月,QQ:9537075 TEL:13787877670
* Email:[email protected]
*/
/*這裡是定義類Template*/
class Template
{
/* 如果設置了,則輸出參數 */
var $classname = "Template";
var $debug = false; //是否調試
var $root = ".";//root為模板文件的存放目錄
var $file = array(); //包含了所有的模板文件名和模板名的數組
var $varkeys = array(); //存放文本元素的鍵名
var $varvals = array(); //存放文本元素的值
var $unknowns = "remove";
/* "remove" => 刪除未定義的變量 "comment" => 將未定義的變量變成注釋 "keep" => 保留未定義的變量 */
var $halt_on_error = "yes";
/* "yes" => 退出 "report" => 報告錯誤,繼續運行* "no" => 忽略錯誤*/
var $last_error = "";
/* 上一次的錯誤保存在這裡 */
/* public: 構造函數
* root: 模板目錄
* unknowns: 如何處理未知的變量(譯者:變量定義為{ name })
*/
/*這裡是定義函數Template*/
function Template($root = ".", $unknowns = "remove")
{
if ($this->debug & 4)
{
echo "<p><b>模板:</b> root = $root, unknowns = $unknowns</p>\n";
}
$this->set_root($root);//默認將文件目錄設置為相同的目錄
$this->set_unknowns($unknowns);//unknowns默認設置為"remove"
}
/*這裡是函數set_root*/
function set_root($root)
{
if ($this->debug & 4)
{
echo "<p><b>設置根目錄:</b> root = $root</p>\n";
}
if (!is_dir($root))
{
$this->halt("設置根目錄: $root 不是一個無效的目錄.");
return false;
}
$this->root = $root;
return true;
}
//這裡是函數set_unknowns,即對未知變量的處理
function set_unknowns($unknowns = "remove")
{
if ($this->debug & 4)
{
echo "<p><b>未知的:</b> 未知 = $unknowns</p>\n";
}
$this->unknowns = $unknowns;
}
/*這裡是函數set_file.......................................................*/
//該方法在數組file中根據$varname提供的鍵名加入值
function set_file($varname, $filename = "")
{
if (!is_array($varname))//如果varname是數組
{
if ($this->debug & 4)
{
echo "<p><b>設置文件:</b> (with scalar) varname = $varname, filename = $filename</p>\n";
}
if ($filename == "")//如果文件名為空,輸出錯誤
{
$this->halt("設置文件:變量名 $varname 文件名是空的.");
return false;
}
$this->file[$varname] = $this->filename($filename);
}
else
{
reset($varname);//將varname的鍵名作為file數組的鍵名
//將鍵名對應的值作為file數組的值
while(list($v, $f) = each($varname))
{
if ($this->debug & 4)
{
echo "<p><b>set_file:</b> (with array) varname = $v, filename = $f</p>\n";
}
if ($f == "")
{
$this->halt("set_file: For varname $v filename is empty.");
return false;
}
$this->file[$v] = $this->filename($f);
}
}
return true;
}
//該方法取出某個父模板文件中的一個子模板
//將其作為塊來加載
//並用另外一個模板變量取代之
/* public: set_file(array $filelist)
* comment: 設置多個模板文件
* filelist: (句柄,文件名)數組
* public: set_file(string $handle, string $filename)
* comment: 設置一個模板文件
* handle: 文件的句柄
* filename: 模板文件名
*/
function set_block($parent, $varname, $name = "") {
if ($this->debug & 4) {
echo "<p><b>set_block:</b> parent = $parent, varname = $varname, name = $name</p>\n";
}
if (!$this->loadfile($parent)) {
$this->halt("set_block: unable to load $parent.");
return false;
}
if ($name == "") {
$name = $varname;//如果沒有指定模板變量的值就用子模板名作為模板變量名
}
$str = $this->get_var($parent);
$reg = "/[ \t]*<!--\s+BEGIN $varname\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $varname\s+-->\s*?\n?/sm";
preg_match_all($reg, $str, $m);
$str = preg_replace($reg, "{ " . "$name }", $str);
$this->set_var($varname, $m[1][0]);
$this->set_var($parent, $str);
return true;
}
//該方法向Varname和varkeys數組中添加新的鍵一值對
/* public: set_var(array $values)
* values: (變量名,值)數組
* public: set_var(string $varname, string $value)
* varname: 將被定義的變量名
* value: 變量的值
*/
function set_var($varname, $value = "", $append = false) {
if (!is_array($varname))//如果不是陣列
{
if (!empty($varname)) //如果是空的
{
if ($this->debug & 1) {
printf("<b>set_var:</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varname, htmlentities($value));
}
$this->varkeys[$varname] = "/".$this->varname($varname)."/";
if ($append && isset($this->varvals[$varname])) {
$this->varvals[$varname] .= $value;
} else {
$this->varvals[$varname] = $value;
}
}
} else {
reset($varname);
while(list($k, $v) = each($varname)) {
if (!empty($k)) {
if ($this->debug & 1) {
printf("<b>set_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $k, htmlentities($v));
}
$this->varkeys[$k] = "/".$this->varname($k)."/";
if ($append && isset($this->varvals[$k])) {
$this->varvals[$k] .= $v;
} else {
$this->varvals[$k] = $v;
}
}
}
}
}
//定義函數clear_var
function clear_var($varname) {
if (!is_array($varname))//如果varname不是陣列
{
if (!empty($varname)) //如果是空的
{
if ($this->debug & 1) {
printf("<b>clear_var:</b> (with scalar) <b>%s</b><br>\n", $varname);
}
$this->set_var($varname, "");
}
} else {
reset($varname);
while(list($k, $v) = each($varname)) {
if (!empty($v)) {
if ($this->debug & 1) {
printf("<b>clear_var:</b> (with array) <b>%s</b><br>\n", $v);
}
$this->set_var($v, "");
}
}
}
}
/*這裡是函數unset_var,刪除變量的定義*/
function unset_var($varname) {
if (!is_array($varname)) {
if (!empty($varname)) {
if ($this->debug & 1) {
printf("<b>unset_var:</b> (with scalar) <b>%s</b><br>\n", $varname);
}
unset($this->varkeys[$varname]);
unset($this->varvals[$varname]);
}
} else {
reset($varname);
while(list($k, $v) = each($varname)) {
if (!empty($v)) {
if ($this->debug & 1) {
printf("<b>unset_var:</b> (with array) <b>%s</b><br>\n", $v);
}
unset($this->varkeys[$v]);
unset($this->varvals[$v]);
}
}
}
}
//將模板文件中的變化內容替換成確定內容的操作,實現數據和顯示的分離
function subst($varname) {
$varvals_quoted = array();
if ($this->debug & 4) {
echo "<p><b>subst:</b> varname = $varname</p>\n";
}
if (!$this->loadfile($varname)) //裝載模板文件,如果出錯就停止
{
$this->halt("subst: unable to load $varname.");
return false;
}
reset($this->varvals);
while(list($k, $v) = each($this->varvals)) {
$varvals_quoted[$k] = preg_replace(array('/\\\\/', '/\$/'), array('\\\\\\', '\\\\$'), $v);
}
//讀入文件內容到字符串中並在下行對已知鍵值進行替換並返回結果
$str = $this->get_var($varname);
$str = preg_replace($this->varkeys, $varvals_quoted, $str);
return $str;
}
//同subst,只是直接輸出結果
function psubst($varname) {
if ($this->debug & 4) {
echo "<p><b>psubst:</b> varname = $varname</p>\n";
}
print $this->subst($varname);
return false;
}
//將varname代表的一個或多個文件中的內容完成替換
//存放在target為鍵值的varvals數組無素中或追加到其後
//返回值和sub相同
function parse($target, $varname, $append = false) {
if (!is_array($varname)) {
if ($this->debug & 4) {
echo "<p><b>parse:</b> (with scalar) target = $target, varname = $varname, append = $append</p>\n";
}
$str = $this->subst($varname);
if ($append) {
$this->set_var($target, $this->get_var($target) . $str);
} else {
$this->set_var($target, $str);
}
} else {
reset($varname);
while(list($i, $v) = each($varname)) {
if ($this->debug & 4) {
echo "<p><b>parse:</b> (with array) target = $target, i = $i, varname = $v, append = $append</p>\n";
}
$str = $this->subst($v);
if ($append) {
$this->set_var($target, $this->get_var($target) . $str);
} else {
$this->set_var($target, $str);
}
}
}
if ($this->debug & 4) {
echo "<p><b>parse:</b> completed</p>\n";
}
return $str;
}
//同parse方法,只是該方法將結果輸出
function pparse($target, $varname, $append = false) {
if ($this->debug & 4) {
echo "<p><b>pparse:</b> passing parameters to parse...</p>\n";
}
print $this->finish($this->parse($target, $varname, $append));
return false;
}
//返回所有的鍵一值對中的值所組成的數組
function get_vars() {
if ($this->debug & 4) {
echo "<p><b>get_vars:</b> constructing array of vars...</p>\n";
}
reset($this->varkeys);
while(list($k, $v) = each($this->varkeys)) {
$result[$k] = $this->get_var($k);
}
return $result;
}
//根據鍵名返回對應的鍵一值勤對應的值
function get_var($varname) {
if (!is_array($varname)) //如果不是陣列
{
if (isset($this->varvals[$varname])) //如果變量不存在
{
$str = $this->varvals[$varname];
} else {
$str = "";
}
if ($this->debug & 2) {
printf ("<b>get_var</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varname, htmlentities($str));
}
return $str;
} else {
reset($varname);
while(list($k, $v) = each($varname)) {
if (isset($this->varvals[$v])) {
$str = $this->varvals[$v];
} else {
$str = "";
}
if ($this->debug & 2) {
printf ("<b>get_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $v, htmlentities($str));
}
$result[$v] = $str;
}
return $result;
}
}
//如果加載文件失敗,返回錯誤並停止
function get_undefined($varname) {
if ($this->debug & 4) {
echo "<p><b>get_undefined:</b> varname = $varname</p>\n";
}
if (!$this->loadfile($varname)) {
$this->halt("get_undefined: unable to load $varname.");
return false;
}
preg_match_all("/{ ([^ \t\r\n }]+) }/", $this->get_var($varname), $m);
$m = $m[1];
//如果無法找到匹配的文本,返回錯誤
if (!is_array($m)) {
return false;
}
//如果能找到大括號中的非空字符,則將其值作為鍵值,組成一個新的數組
reset($m);
while(list($k, $v) = each($m)) {
if (!isset($this->varkeys[$v])) {
if ($this->debug & 4) {
echo "<p><b>get_undefined:</b> undefined: $v</p>\n";
}
$result[$v] = $v;
}
}
//如是該數組不為空就返回該數組,否則就返回錯誤
if (count($result)) {
return $result;
} else {
return false;
}
}
//完成對str的最後的處理工作,利用類的屬性unknowns來確定對模板中無法處理的動態部分的處理方法
function finish($str) {
switch ($this->unknowns) {
case "keep": //保持不變
break;
case "remove": //刪除所有的非控制符
$str = preg_replace('/{ [^ \t\r\n }]+ }/', "", $str);
break;
case "comment"://將大括號中的HTML注釋
$str = preg_replace('/{ ([^ \t\r\n }]+) }/', "<!-- Template variable \\1 undefined -->", $str);
break;
}
return $str;
}
//將參數變量對诮的數組中的值處理後輸出
function p($varname) {
print $this->finish($this->get_var($varname));
}
//將參數變量對應的數組中的值處理後返回
function get($varname) {
return $this->finish($this->get_var($varname));
}
//檢查並補充給定的文件名
function filename($filename) {
if ($this->debug & 4) {
echo "<p><b>filename:</b> filename = $filename</p>\n";
}
if (substr($filename, 0, 1) != "/")
//如果文件名不是以斜槓開頭,則表示是相對路徑,將其補充為完整的絕對路徑
{
$filename = $this->root."/".$filename;
}
//如果文件不存在
if (!file_exists($filename)) {
$this->halt("filename: file $filename does not exist.");
}
return $filename;//返回文件名
}
//對變量名進行處理,將正則表達式中的敏感字符變為轉義字符,並在變量名兩端加上大括號
function varname($varname) {
return preg_quote("{ ".$varname." }");
}
//該方法根據varname加載文件到鍵一值對中
function loadfile($varname) {
if ($this->debug & 4) {
echo "<p><b>loadfile:</b> varname = $varname</p>\n";
}
if (!isset($this->file[$varname])) //如果沒有指定就返加錯誤
{
// $varname does not reference a file so return
if ($this->debug & 4) {
echo "<p><b>loadfile:</b> varname $varname does not reference a file</p>\n";
}
return true;
}
if (isset($this->varvals[$varname]))//如果已經加載了varname為名柄的文件,直接返回真值
{
if ($this->debug & 4) {
echo "<p><b>loadfile:</b> varname $varname is already loaded</p>\n";
}
return true;
}
$filename = $this->file[$varname];//句柄有效則取出對應的文件名
$str = implode("", @file($filename));//將文件的每一行連接成一個字符串
if (empty($str)) //字符串空說明文件空或者不存在,返回錯誤
{
$this->halt("loadfile: While loading $varname, $filename does not exist or is empty.");
return false;
}
if ($this->debug & 4) {
printf("<b>loadfile:</b> loaded $filename into $varname<br>\n");
}
$this->set_var($varname, $str);//如果文件不為空,用$varname作為句柄,str為變量名
//向鍵值對中添加新的鍵值
return true;
}
//將分析結果保存到文件中去
function savetofile ($dir,$varname){
$data=$this->finish($this->get_var($varname));
$fp=fopen($dir,"w+");
fwrite($fp,$data);
}
//清除已賦值數組
function renew(){
$this->varkeys=array();
$this->varvals=array();
$this->file=array();
}
//出錯提示並終止程序運行
function halt($msg) {
$this->last_error = $msg;
if ($this->halt_on_error != "no") {
$this->haltmsg($msg);
}
if ($this->halt_on_error == "yes") {
die("<b>終止.</b>");
}
return false;
}
//出錯提示
function haltmsg($msg) {
printf("<b>模板錯誤:</b> %s<br>\n", $msg);
}
}
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved