在編寫PHP模板引擎工具類時(shí),以前常用的一個(gè)正則替換函數(shù)為 preg_replace(),加上正則修飾符 /e,就能夠執(zhí)行強(qiáng)大的回調(diào)函數(shù),實(shí)現(xiàn)模板引擎編譯(其實(shí)就是字符串替換)。
詳情介紹參考博文:PHP函數(shù)preg_replace() 正則替換所有符合條件的字符串
應(yīng)用舉例如下:
<?php /** * 模板解析類 */ class Template { public function compile($template) { // if邏輯 $template = preg_replace("/\<\!\-\-\{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é)果為:
xxx<?php if ($user['userName']) { ?>yyy<?php if ($user[\"password\"]) { ?>zzz
仔細(xì)觀察,發(fā)現(xiàn) $user["password"] 中的雙引號被轉(zhuǎn)義了,這不是我們想要的結(jié)果。
為了能夠正常輸出,還必須反轉(zhuǎn)義一下,但是,如果字符串中本身含有反轉(zhuǎn)義雙引號的話,我們此時(shí)反轉(zhuǎn)義,原本的反轉(zhuǎn)義就變成了非反轉(zhuǎn)義了,這個(gè)結(jié)果又不是我們想要的,所以說這個(gè)函數(shù)在這方面用的不爽!
后來,發(fā)現(xiàn)一個(gè)更專業(yè)級的 正則替換回調(diào)函數(shù) preg_replace_callback()。
php7中,preg_replace()不再支持"\e" (PREG_REPLACE_EVAL),需要使用preg_replace_callback()來代替。
看例子來說吧
$content = preg_replace("/{#(.+?)}/eis", '$lang[\'\\1\']', $content) ;
這是原來的代碼,在php7里不能被正確執(zhí)行。
$content = preg_replace_callback("/{#(.+?)}/is", function($r)use($lang){ return $lang[$r[1]]; }, $content);
這是改過之后的代碼
需要注意:
1. preg_replace_callback第二個(gè)參數(shù),callback()函數(shù)里,如果需要使用外部的變量,可以使用function callback($matchs)use($xxx){}
2. callback() 回調(diào)函數(shù)里,return 值會(huì)替換匹配到的內(nèi)容
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] ) 本函數(shù)的行為幾乎和 preg_replace() 一樣,除了不是提供一個(gè) replacement 參數(shù),而是指定一個(gè) callback 函數(shù)。該函數(shù)將以目標(biāo)字符串中的匹配數(shù)組作為輸入?yún)?shù),并返回用于替換的字符串。
回調(diào)函數(shù) callback:
一個(gè)回調(diào)函數(shù),在每次需要替換時(shí)調(diào)用,調(diào)用時(shí)函數(shù)得到的參數(shù)是從subject 中匹配到的結(jié)果。回調(diào)函數(shù)返回真正參與替換的字符串。這是該回調(diào)函數(shù)的簽名:
string handler ( array $matches )
像上面所看到的,回調(diào)函數(shù)通常只有一個(gè)參數(shù),且是數(shù)組類型。
羅列一些有關(guān)preg_replace_callback()函數(shù)的實(shí)例:
Example #1 preg_replace_callback() 和 匿名函數(shù)
<?php /* 一個(gè)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ù)是個(gè)匿名函數(shù),在PHP5.3中,通過關(guān)鍵字use,支持給匿名函數(shù)傳多個(gè)參數(shù),如下所示:
<?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ù)
<?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]是第一個(gè)捕獲子組的匹配 // 以此類推 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,第二個(gè)參數(shù) 像這樣 array($this, 'replace') :
<?php class test_preg_callback{private function process($text){ $reg = "/\{([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,第二個(gè)參數(shù)像這樣 "self::replace" : 注意,也可以是 array($this, 'replace')。
<?php class test_preg_callback{private function process($text){ $reg = "/\{([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é)到的知識點(diǎn),把模板引擎類改造如下:
<?php /** * 模板解析類 */ class Template {public function compile($template) {
// if邏輯 $template = preg_replace_callback("/\<\!\-\-\{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é)果為:
xxx<?php if ($user['userName']) { ?>yyy<?php if ($user["password"]) { ?>zzz
正是我們想要的結(jié)果,雙引號沒有被反轉(zhuǎn)義!
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇