2017-06-17 30 views
0

我正在使用wordpress插件Ultimate Membership Pro。我的代碼中有一個小警告。我無法弄清楚問題是什麼。警告:preg_replace_callback():需要參數2,才能成爲有效的回調

我試着檢查PHP手冊,也做這個主題的研究,但無法解決它。任何人有一個想法如何解決它?只需要一個提示。

PHP代碼:

原始代碼:

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/>'; 
        } 
       } 
      } 
     } 
    } 

錯誤:

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

更新與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); 

對於更新的輸出是這樣的:

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 

我的數組是$數據 - >歷史

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: 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 
+0

錯誤消息非常簡單。我想你只需要'preg_replace'。 – smarx

+0

@smarx我把它'preg_replace'改爲'preg_replace_callback',並給我警告我的PHP不支持'preg_replace' php 7.x –

+0

根據http://php.net/manual/en/function.preg-replace .php,應該在PHP 7中支持'preg_replace'。什麼是確切的錯誤信息? – smarx

回答

1

如果我說得對,你想要做什麼,這可能是你在找什麼:

$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); 

請注意,我已經從搜索模式扔下e修改,然後通過在作爲第二參數的函數中。此功能會進行匹配並返回您想要的替換。

(另請注意,對於給定的輸入,此替換不會改變任何內容。)

+0

謝謝!工作就像魅力:) –

相關問題