2012-08-25 52 views
2

我試圖在互聯網上部署我的網站,以便在真實環境中進行測試。它是某種文本編輯器,用戶可以使用正則表達式和用戶定義的回調函數。PHP preg_replace_callback()和createvalue()與eval()不起作用

我有一些preg_replace_callback()函數的問題。我的託管有PHP版本比5.3更舊,我不能在我的代碼中使用匿名函數(我在本地有PHP 5.4)。所以,我必須重寫代碼的這一部分(其在本地主機正常工作)

$newString = preg_replace_callback(
        '#' . $this->pattern . '#' . $this->modifiers, 
        function($match) 
        { 
         return eval('return ' . $this->replacement . ';'); 
        }, 
        $string); 

在這一點上,我不談論使用eval()的危險性 - 這個問題將支付適當關注一點稍後(「禁止」的單詞列表檢查等)。問題是,我的嘗試下面

$replacement = $this->replacement; 
$newString = preg_replace_callback(
    '#' . $this->pattern . '#' . $this->modifiers, 
    create_function('$match', ' 
        global $replacement; 
        return eval(\'return \' . $replacement . \';\'); 
        '), 
        $string); 

不工作,沒有錯誤發生。我的代碼有什麼問題?

任何幫助將不勝感激。


新信息。我已經試過這

Class A 
{ 
    public function check() 
    { 
     $string = 'series 3-4'; 
     $pattern = 'series[ ]*(\d+)[ ]*-[ ]*(\d+)'; 
     $modifiers = 'um'; 
     $replacement = '$match[2] == $match[1] + 1 ? "series $match[1] and $match[2]" : "series $match[1]-$match[2]"'; 
     $newString = preg_replace_callback(
          '#' . $pattern . '#' . $modifiers, 
          create_function('$match', ' 
          global $replacement; 
          echo $replacement; 
          return eval(\'return \' . $replacement . \';\'); 
          '), 
          $string); 
     echo $newString; 
    } 
} 

$a = new A; 
$a->check();//get nothing 

,並發現$更換內部create_function()是空的。但是,當我用同樣的create_function()類$更換外不爲空:

$string = 'series 3-4'; 
$pattern = 'series[ ]*(\d+)[ ]*-[ ]*(\d+)'; 
$modifiers = 'um'; 
$replacement = '$match[2] == $match[1] + 1 ? "series $match[1] and $match[2]" : "series $match[1]-$match[2]"'; 
$newString = preg_replace_callback(
       '#' . $pattern . '#' . $modifiers, 
       create_function('$match', ' 
       global $replacement; 
       echo $replacement . "<br/>"; 
       return eval(\'return \' . $replacement . \';\'); 
       '), 
       $string); 
echo $newString; 
//$match[2] == $match[1] + 1 ? "series $match[1] and $match[2]" : "series $match[1]-$match[2]" 
//series 3 and 4 

回答

1

這工作,插入$replacement字符串值格式正確PHP源入函數的源代碼:

function check() 
{ 
    $string = 'series 3-4'; 
    $pattern = 'series[ ]*(\d+)[ ]*-[ ]*(\d+)'; 
    $modifiers = 'um'; 
    $replacement = '$match[2] == $match[1] + 1 ? "series $match[1] and $match[2]" : "series $match[1]-$match[2]"'; 
    $newString = preg_replace_callback(
         '#' . $pattern . '#' . $modifiers, 
         create_function('$match', ' 
          return eval("return " . '.var_export($replacement,true).' . ";"); 
         '), 
         $string); 
    echo $newString; 
} 
check(); // prints "series 3 and 4" 
?> 

但是爲什麼還要在代碼將和荷蘭國際集團eval它,當你可以只要把它直接作用來源:

<? 
function check() 
{ 
    $string = 'series 3-4'; 
    $pattern = 'series[ ]*(\d+)[ ]*-[ ]*(\d+)'; 
    $modifiers = 'um'; 
    $replacement = '$match[2] == $match[1] + 1 ? "series $match[1] and $match[2]" : "series $match[1]-$match[2]"'; 
    $newString = preg_replace_callback(
         '#' . $pattern . '#' . $modifiers, 
         create_function('$match', ' 
          return '.$replacement.'; 
         '), 
         $string); 
    echo $newString; 
} 
check(); // prints "series 3 and 4" 
?> 
+0

是的,真的。現在我發現我根本不需要使用eval()。謝謝! – Placido

5

您可以代替lambda函數的使用方法:

Class A 
{ 
    private $replacement; 
    public function check() 
    { 
     $string = 'series 3-4'; 
     $pattern = 'series[ ]*(\d+)[ ]*-[ ]*(\d+)'; 
     $this->replacement = '$match[2] == $match[1] + 1 ? "series $match[1] and $match[2]" : "series $match[1]-$match[2]"'; 
     $modifiers = 'um'; 
     $newString = preg_replace_callback(
          '#' . $pattern . '#' . $modifiers, 
          array($this, 'replacementCallback'), 
          $string); 
     echo $newString; 
    } 

    private function replacementCallback($match) 
    { 
     return eval('return ' . $this->replacement . ';'); 
    } 
} 

$a = new A; 
$a->check(); 
+0

IIRC的回調應該是'public' – zerkms

+0

我發佈之前執行的代碼。 PHP 5.3.13。編輯:剛剛檢查5.2.17。也適用。 – Bugs

+0

非常感謝! – Placido