1.首先preg_replace()是執行一個正則表達式的搜索和替換的函數
preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed
搜索subject中匹配pattern的部分, 以replacement進行替換。
2.preg_replace()在php5.5是廢棄了/e修飾符,需要使用 preg_replace_callback()作爲替代
3.preg_replace_callback()用法:
preg_replace_callback — 執行一個正則表達式搜索並且使用一個回調進行替換
說明:
使用:
if (!defined('ECS_ADMIN'))
{
$source = $this->smarty_prefilter_preCompile($source);
}
$source = preg_replace("/<\?[^><]+\?>|<\%[^><]+\%>|<script[^>]+language[^>]*=[^>]*php[^>]*>[^><]*<\/script\s*>/iU", "", $source);
return preg_replace_callback("/{([^\}\{\n]*)}/", function($r) { return $this->select($r[1]); }, $source);
其中第二個參數function爲需要執行的php代碼。注:需要使用retrun返回
function select($tag)
{
$a="123456";
return $a;
}