带有eval()的PHP preg_replace_callback()和create_function()不起作用

Pla*_*ido 2 php oop eval preg-replace-callback create-function

我正在尝试在Internet上部署我的网站,以便在真实环境中对其进行测试.它是某种文本编辑器,用户可以使用正则表达式和用户定义的回调函数.

我有一些preg_replace_callback()函数的问题.我的托管有早于5.3的PHP版本,我不能在我的代码中使用匿名函数(我在localhost上有PHP 5.4).所以我必须重写这部分代码(在localhost上正常工作)

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

在这一点上,我不是在谈论使用eval()的危险 - 这个问题稍后会得到适当的关注("禁止"单词列表检查等).问题是我的尝试如下

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

不起作用,没有错误发生.我的代码出了什么问题?

任何帮助将非常感激.


新信息.我试过这个

 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
Run Code Online (Sandbox Code Playgroud)

并发现create_function()内的$ replacement为空.但是当我在类$外使用相同的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
Run Code Online (Sandbox Code Playgroud)

Bug*_*ugs 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();
Run Code Online (Sandbox Code Playgroud)