php5.3升至5.6 preg_replace()函數/e修飾符報錯處理

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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章