簡單復雜的模板替換函數(shù)-新手
作者: 鄭曉 分類: PHP 發(fā)布于: 2012-03-23 09:37 瀏覽:5,676 評論(2)
花了大半天的時間寫的幾個正則。。。這個正則表達式以前基本沒用過,不太熟練,花了大半天才寫了這么幾行。。。而且感覺寫的復雜了。。
',$content);
//替換函數(shù) 函數(shù)名(帶雙引號的參數(shù) 或 帶單引號的參數(shù) 或 日期格式參數(shù) 或 變量 或數(shù)組)
$content = preg_replace("/{tpl:\s?(([a-z_]+)\((\"[a-zA-Z_-]+\",?|'[a-zA-Z_-]+',?|\"[a-zA-Z\s\:-]+\",?|(\\$[a-zA-Z_]+)(\[([a-zA-Z_-]+)\])?,?)*\))}/",'',$content);
//替換loop循環(huán)開始
$content = preg_replace("/{tpl:loop\s(\\$[a-zA-Z_]+)\s(\\$[a-zA-Z_]+)\s(\\$[a-zA-Z_]+)}/",'\\3) {?>',$content);
//替換/loop循環(huán)結束
$content = preg_replace("/{tpl:\/loop}/",'',$content);
//替換循環(huán)數(shù)組變量
$content = preg_replace("/{tpl:(\\$[a-zA-Z_]+)(\[([a-zA-Z_-]+)\])}/",'',$content);
//替換if
$content = preg_replace("/{tpl:(if\s(\\$[a-zA-Z_]+)(\[([a-zA-Z_-]+)\])?([=<>!]{1,2}(\"[0-9a-zA-Z_-]+\"|'[0-9a-zA-Z_-]+'|[0-9a-zA-Z]+|(\\$[a-zA-Z_]+)(\[([a-zA-Z_-]+)\])?)))}/",'',$content);
//替換else
$content = preg_replace("/{tpl:else}/",'',$content);
//替換/if
$content = preg_replace("/{tpl:\/if}/",'',$content);
return $content;
}
?>
本文采用知識共享署名-非商業(yè)性使用 3.0 中國大陸許可協(xié)議進行許可,轉載時請注明出處及相應鏈接。
本文永久鏈接: http://yjfs.org.cn/the-simple-and-complex-the-template-the-replace-function-novice.html
很久就想學,沒開始。慚愧
哈哈~我也是剛學。。就這點兒東西還弄了大半天 ??