當(dāng)前位置:博客首頁(yè)>>PHP >> 閱讀正文

簡(jiǎn)單復(fù)雜的模板替換函數(shù)-新手

作者: 鄭曉 分類: PHP 發(fā)布于: 2012-03-23 09:37 瀏覽:5,556 評(píng)論(2)


花了大半天的時(shí)間寫的幾個(gè)正則。。。這個(gè)正則表達(dá)式以前基本沒(méi)用過(guò),不太熟練,花了大半天才寫了這么幾行。。。而且感覺(jué)寫的復(fù)雜了。。

',$content);

//替換函數(shù) 函數(shù)名(帶雙引號(hào)的參數(shù) 或 帶單引號(hào)的參數(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)結(jié)束
$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;
}
?>

? ? ? ?

本文采用知識(shí)共享署名-非商業(yè)性使用 3.0 中國(guó)大陸許可協(xié)議進(jìn)行許可,轉(zhuǎn)載時(shí)請(qǐng)注明出處及相應(yīng)鏈接。

本文永久鏈接: http://www.yjfs.org.cn/the-simple-and-complex-the-template-the-replace-function-novice.html

簡(jiǎn)單復(fù)雜的模板替換函數(shù)-新手:目前有2 條留言

用戶評(píng)論頭像 東陽(yáng)高復(fù)發(fā)表于 2012年03月24日 07:08[回復(fù)]

很久就想學(xué),沒(méi)開始。慚愧

    用戶評(píng)論頭像 鄭曉發(fā)表于 2012年03月24日 17:44[回復(fù)]

    哈哈~我也是剛學(xué)。。就這點(diǎn)兒東西還弄了大半天 ??

發(fā)表評(píng)論

change vcode