douyanlu7380 2016-06-26 21:07
浏览 76
已采纳

preg_replace_callback():要求参数2成为有效的回调函数

$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...

I'm not quite sure what to do here. Any help from someone smarter than I would be greatly appreciated...

  • 写回答

1条回答 默认 最新

  • duanniedang3946 2016-06-26 22:09
    关注

    The First issue here is Intent. Well, the 2nd Argument passed to preg_replace_callback($arg1, $arg2...) is expected to be a Callable. That is why you have that error. It is unclear where you are going with your code but perhaps the code below could throw more light and help you either rethink/clarify your question, intent + goal or revisit your code. Consider this:

    <?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!!! '
    

    Notice that in the code above, the 2nd Argument passed to preg_replace_callback is a function though passed as a REFERENCE to the 'entity_fix' Key of the array: $func. This was intended to highlight the fact that it is also possible to pass the 2nd Argument in such a manner. It is hoped that this here gives you a little tip to kick off ;-)

    Good Luck!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?