|
在編寫php模板引擎工具類時,以前常用的一個正則替換函數(shù)為 preg_replace(),加上正則修飾符 /e,就能夠執(zhí)行強大的回調(diào)函數(shù),實現(xiàn)模板引擎編譯(其實就是字符串替換)。
詳情介紹參考博文:php函數(shù)preg_replace() 正則替換所有符合條件的字符串
應(yīng)用舉例如下:
復(fù)制代碼 代碼如下:
<?php
/**
* 模板解析類
*/
class Template {
public function compile($template) {
// if邏輯
$template = preg_replace("http://</!/-/-/{if/s+(.+?)/}/-/-/>/e", "/$this->ifTag('//1')", $template);
return $template;
}
/**
* if 標(biāo)簽
*/
protected function ifTag($str) {
//$str = stripslashes($str); // 去反轉(zhuǎn)義
return '<?php if (' . $str . ') { ?>';
}
}
$template = 'xxx<!--{if $user[/'userName/']}-->yyy<!--{if $user["password"]}-->zzz';
$tplComplier = new Template();
$template = $tplComplier->compile($template);
echo $template;
?>
輸出結(jié)果為:
復(fù)制代碼 代碼如下:
xxx<?php if ($user['userName']) { ?>yyy<?php if ($user[/"password/"]) { ?>zzz
仔細(xì)觀察,發(fā)現(xiàn) $user["password"] 中的雙引號被轉(zhuǎn)義了,這不是我們想要的結(jié)果。
為了能夠正常輸出,還必須反轉(zhuǎn)義一下,但是,如果字符串中本身含有反轉(zhuǎn)義雙引號的話,我們此時反轉(zhuǎn)義,原本的反轉(zhuǎn)義就變成了非反轉(zhuǎn)義了,這個結(jié)果又不是我們想要的,所以說這個函數(shù)在這方面用的不爽!
后來,發(fā)現(xiàn)一個更專業(yè)級的 正則替換回調(diào)函數(shù) preg_replace_callback()。
復(fù)制代碼 代碼如下:
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )
本函數(shù)的行為幾乎和 preg_replace() 一樣,除了不是提供一個 replacement 參數(shù),而是指定一個 callback 函數(shù)。該函數(shù)將以目標(biāo)字符串中的匹配數(shù)組作為輸入?yún)?shù),并返回用于替換的字符串。
回調(diào)函數(shù) callback:
一個回調(diào)函數(shù),在每次需要替換時調(diào)用,調(diào)用時函數(shù)得到的參數(shù)是從subject 中匹配到的結(jié)果?;卣{(diào)函數(shù)返回真正參與替換的字符串。這是該回調(diào)函數(shù)的簽名:
復(fù)制代碼 代碼如下:
string handler ( array $matches )
像上面所看到的,回調(diào)函數(shù)通常只有一個參數(shù),且是數(shù)組類型。
羅列一些有關(guān)preg_replace_callback()函數(shù)的實例:
Example #1 preg_replace_callback() 和 匿名函數(shù)
復(fù)制代碼 代碼如下:
<?php
/* 一個unix樣式的命令行過濾器,用于將段落開始部分的大寫字母轉(zhuǎn)換為小寫。 */
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
$line = fgets($fp);
$line = preg_replace_callback(
'|<p>/s*/w|',
function ($matches) {
return strtolower($matches[0]);
},
$line
);
echo $line;
}
fclose($fp);
?>
如果回調(diào)函數(shù)是個匿名函數(shù),在php5.3中,通過關(guān)鍵字use,支持給匿名函數(shù)傳多個參數(shù),如下所示:
復(fù)制代碼 代碼如下:
<?php
$string = "Some numbers: one: 1; two: 2; three: 3 end";
$ten = 10;
$newstring = preg_replace_callback(
'/(//d+)/',
function($match) use ($ten) { return (($match[0] + $ten)); },
$string
);
echo $newstring;
#prints "Some numbers: one: 11; two: 12; three: 13 end";
?>
Example #2 preg_replace_callback() 和 一般函數(shù)
復(fù)制代碼 代碼如下:
<?php
// 將文本中的年份增加一年.
$text = "April fools day is 04/01/2002/n";
$text.= "Last christmas was 12/24/2001/n";
// 回調(diào)函數(shù)
function next_year($matches) {
// 通常: $matches[0]是完成的匹配
// $matches[1]是第一個捕獲子組的匹配
// 以此類推
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(/d{2}//d{2}/)(/d{4})|",
"next_year",
$text);
?>
Example #3 preg_replace_callback() 和 類方法
如何在類的內(nèi)部調(diào)用非靜態(tài)函數(shù)?你可以按如下操作:
對于 php 5.2,第二個參數(shù) 像這樣 array($this, 'replace') :
復(fù)制代碼 代碼如下:
<?php
class test_preg_callback{
private function process($text){
$reg = "http://{([0-9a-zA-Z/- ]+)/:([0-9a-zA-Z/- ]+):?/}/";
return preg_replace_callback($reg, array($this, 'replace'), $text);
}
private function replace($matches){
if (method_exists($this, $matches[1])){
return @$this->$matches[1]($matches[2]);
}
}
}
?>
對于 php5.3,第二個參數(shù)像這樣 "self::replace" :
注意,也可以是 array($this, 'replace')。
復(fù)制代碼 代碼如下:
<?php
class test_preg_callback{
private function process($text){
$reg = "http://{([0-9a-zA-Z/- ]+)/:([0-9a-zA-Z/- ]+):?/}/";
return preg_replace_callback($reg, "self::replace", $text);
}
private function replace($matches){
if (method_exists($this, $matches[1])){
return @$this->$matches[1]($matches[2]);
}
}
}
?>
根據(jù)上面所學(xué)到的知識點,把模板引擎類改造如下:
復(fù)制代碼 代碼如下:
<?php
/**
* 模板解析類
*/
class Template {
public function compile($template) {
// if邏輯
$template = preg_replace_callback("http://</!/-/-/{if/s+(.+?)/}/-/-/>/", array($this, 'ifTag'), $template);
return $template;
}
/**
* if 標(biāo)簽
*/
protected function ifTag($matches) {
return '<?php if (' . $matches[1] . ') { ?>';
}
}
$template = 'xxx<!--{if $user[/'userName/']}-->yyy<!--{if $user["password"]}-->zzz';
$tplComplier = new Template();
$template = $tplComplier->compile($template);
echo $template;
?>
輸出結(jié)果為:
復(fù)制代碼 代碼如下:
xxx<?php if ($user['userName']) { ?>yyy<?php if ($user["password"]) { ?>zzz
正是我們想要的結(jié)果,雙引號沒有被反轉(zhuǎn)義!
php技術(shù):PHP正則替換函數(shù)preg_replace和preg_replace_callback使用總結(jié),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。