duanhuoyao7011 2017-06-17 09:23
浏览 283
已采纳

警告:preg_replace_callback():需要参数2,才能成为有效的回调

I am using wordpress plugin Ultimate Membership Pro. I have a small warning in my code. I can't figure out what the issue is.

I tried checking php manual and also doing research on this topic but can't solve it. Anyone have an idea how to solve it? Just need a hint.

Php Code:

Original Code:

if (!empty($data->history)){
        //print the history
        $dat = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data->history);
        $dat = unserialize($dat);       
        if (isset($dat) && is_array($dat)){
            foreach ($dat as $k=>$transaction_history_arr){
                if (is_string($transaction_history_arr)){
                    //is json
                    $json = stripslashes($transaction_history_arr);
                    if ($k){
                        echo '<h4>' . date('Y-m-d H:i:s', $k) .'</h4>';
                    }
                    $arr = (array)json_decode($json, true);
                    foreach ($arr as $key=>$value){
                        echo $key.': '.$value.'<br/>';
                    }
                } else {    
                    //is an array
                    if ($k>0){ 
                        echo '<h4>' . date('Y-m-d H:i:s', $k) .'</h4>';
                    }
                    foreach ($transaction_history_arr as $key=>$value){
                        echo $key.' : '.$value.'<br/>';
                    }
                }
            }
        }
    }

Error:

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/html/stock-market/wp-content/plugins/indeed-membership-pro/admin/includes/tabs/list_payments.php on line 44

Updated with preg_replace_callback:

if (!empty($data->history)){
        //print the history
        print_r($data->history);
        $dat = preg_replace_callback('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data->history);
        $dat = unserialize($dat);

For updated Output something like this:

Warning: preg_replace_callback(): Requires argument 2, ''s:'.strlen('$2').':"$2";'', to be a valid callback in /var/www/html/stock-market/wp-content/plugins/indeed-membership-pro/admin/includes/tabs/list_payments.php on line 44 2017-06-17 08:44:49

  ihc_payment_type : paypal  
   details : ss  
   uid : 1 
   level : 2 
   order_id : 21
   amount : 100.00
   currency : USD  
   txn_id : 1_21_1497689089 
   message : success

my array is $data->history :

a:1:{i:1497689089;a:9:{s:16:"ihc_payment_type";s:6:"paypal";s:7:"details";s:2:"ss";s:3:"uid";s:1:"1";s:5:"level";s:1:"2";s:8:"order_id";s:2:"21";s:6:"amount";s:6:"100.00";s:8:"currency";s:3:"USD";s:6:"txn_id";s:15:"1_21_1497689089";s:7:"message";s:7:"success";}}

Warning:

 Warning: preg_replace_callback(): Requires argument 2, ''s:'.strlen('$2').':"$2";'', to be a valid callback in /var/www/html/stock-market/wp-content/plugins/indeed-membership-pro/admin/includes/tabs/list_payments.php on line 47
  • 写回答

1条回答 默认 最新

  • doujunchi1238 2017-06-17 09:38
    关注

    If I'm right about what you're trying to do, this might be what you're looking for:

    $text = 'a:1:{i:1497689089;a:9:{s:16:"ihc_payment_type";s:6:"paypal";s:7:"details";s:2:"ss";s:3:"uid";s:1:"1";s:5:"level";s:1:"2";s:8:"order_id";s:2:"21";s:6:"amount";s:6:"100.00";s:8:"currency";s:3:"USD";s:6:"txn_id";s:15:"1_21_1497689089";s:7:"message";s:7:"success";}}';
    
    $dat = preg_replace_callback('!s:(\d+):"(.*?)";!',
        function ($match) {
            return 's:'.strlen($match[2]).':"'.$match[2].'";';
        },
        $text);
    

    Note that I've dropped the e modifier from the search pattern and then passed in a function as the second parameter. This function takes a match and returns the replacement you want.

    (Also note that for the given input, this replacement doesn't change anything.)

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。