【问题标题】:preg_replace_callback(): Requires argument 2 to be a valid callback inpreg_replace_callback():要求参数 2 是一个有效的回调
【发布时间】:2016-10-28 19:58:27
【问题描述】:
$ent_check = empty($modSettings['disableEntityCheck']) ? 
    array('preg_replace_callback(\'~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e\', \'$func[\\\'entity_fix\\\'](\\\'\\2\\\')\', ', ')') : 
    array('', '');

Warning: preg_replace_callback(): Requires argument 2, '$func['entity_fix']('\2')', to be a valid callback in...

我不太确定在这里做什么。任何比我聪明的人的帮助将不胜感激......

【问题讨论】:

    标签: php regex preg-replace-callback


    【解决方案1】:

    这里的第一个问题是 Intent。那么,传递给preg_replace_callback($arg1, $arg2...) 的第二个参数应该是一个Callable。这就是为什么你有这个错误。目前尚不清楚您的代码将走向何方,但也许下面的代码可以提供更多信息并帮助您重新思考/澄清您的问题、意图 + 目标或重新访问您的代码。考虑一下:

    <?php
        $string         = "&#2510 whatever &#5870 again whatever &#7885";
        $modSettings    = array('disableEntityCheck'=>array());
        $func           = array(
            "fix_stuff"     => function($param=20){ echo $param;},
            "do_stuff"      => function($param=10){ echo $param;},
            "entity_fix"    => function($matches ){ return $matches[0] . "YES!!! ";},
        );
    
        $ent_check  = empty($modSettings['disableEntityCheck']) ? array(preg_replace_callback('#\d#', $func['entity_fix'], $string )) :  array('', '');
        var_dump($ent_check);
    
        // DISPLAYS  
        array (size=1)
            0 => string '&#2YES!!! 5YES!!! 1YES!!! 0YES!!!  whatever &#5YES!!! 8YES!!! 7YES!!! 0YES!!!  again whatever &#7YES!!! 8YES!!! 8YES!!! 5YES!!! '
    

    请注意,在上面的代码中,传递给preg_replace_callback 的第二个参数是一个函数虽然作为一个 REFERENCE 到数组的“entity_fix”键:$func。这是为了强调这样一个事实,即也可以以这种方式传递第二个参数。希望这里能给你一点提示;-)

    祝你好运!!!

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2016-11-08
      相关资源
      最近更新 更多