在PHP中分解大字符串

时间:2014-06-30 09:22:43

标签: php wordpress

我正在尝试让我的用户从我网站上的控制面板编辑他们的订单历史记录。目前,当用户下订单时,执行此脚本:

function process_order_form_handler() {

    //Get form Data from POST array
    $orderArray = json_decode(urldecode($_POST['product_list']),true);
    $orderHistory = $orderArray;

    $comment = $_POST['comments'];
    $delivery = $_POST['next-day-delivery'];

    $id = $_POST['CMP_Member'];

    //Loop through the Array
    if(is_array($orderArray)){
    //Order Code
    $orderRequestCode = date('Y-m-d-H-i-s');
    //Get customer information here - email....
    $user = wp_get_current_user();
    $customerName =  $user->user_firstname . " " . $user->user_lastname;
    $customerEmail = $user->user_email;

    $user_ID = $user->ID;

    //Order history functionality
         add_user_meta($user->ID, "Order", $orderHistory, true);

    //Get the order Items
    echo "This is the order: ";

    $orderContent = "Order Code: " . $orderRequestCode."\r\n";
    $orderContent .= "Customer Name: " . $customerName . "\r\n";
    $orderContent .= "Customer Email: " . $customerEmail . "\r\n";
    if($delivery == 'yes'){
        $orderContent .= "Next Day Delivery: " . $delivery . "\r\n";
    }

    $orderContent .= "Order Items: " . "\r\n";


    foreach($orderArray as $item){
        if($item['bulk'] == 'true'){
        $orderContent .= "Stock Code: " . $item['stockCode'] . " Quantity: " . $item['quantity'] . " Bulk Order";
        if($item['sbulk']){//if true then sharing split bulk
           $orderContent .= " Sharing: " . $item['share'] . " Qty: " . $item['sharequantity'] . "\r\n";
        }else{
            $orderContent .= "\r\n";
        }
        }else{
        $orderContent .= "Stock Code: " . $item['stockCode'] . " Quantity: " . $item['quantity'] . "\r\n";  
        }
    }

    //Comments
    $orderContent .= "Comments: " . "\r\n";
    $orderContent .= $comment;

    $orderContent = wordwrap($orderContent, 70, "\r\n");

    //Once everything is done send to the users email address
    $headers =  'From: sales@mushrooms.ie' . "\r\n" .       
                    'X-Mailer: PHP/' . phpversion();

    // Send to Customer
    mail($customerEmail, 'Order Confirmation', $orderContent, $headers);


    //Send to the Sales dept.
    //mail('xxx@xxx.xxx', 'Order ' . $orderRequestCode, $orderContent, $headers);

    //Create Custom Post of the order
    $my_post = array(
            'post_title' => $orderRequestCode.' - ' . $customerName,
            'post_content' => $orderContent,
            'post_status' => 'publish',
            'post_author' => $user_ID,
            'post_type' => 'orders'
        );

    var_dump($my_post);

    // Insert the post into the database
    wp_insert_post( $my_post);

    //Delete Cookie

        unset($_COOKIE['order_cookie']);
    setcookie('order_cookie', '', time() - 3600);

    //Redirect to Dashboard
    wp_redirect( home_url().'?processed=true');
    exit;
    }
}
?>

当我使用database wordpressget_post($orderID);检索它时,这给了我一个看起来像这样的对象。

object(WP_Post)[325]
  public 'ID' => int 1058
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2014-06-18 08:05:23' (length=19)
  public 'post_date_gmt' => string '2014-06-18 08:05:23' (length=19)
  public 'post_content' => string 'Order Code: 2014-06-18-08-05-20
Customer Name: Jack Coldrick
Customer Email: jack@xxx.ie
Next Day Delivery: yes
Order Items: 
Stock Code: G992/75BLK Quantity: 10
Stock Code: NETT FILM JMC Quantity: 6
Stock Code: WW42 Quantity: 4
Comments: 
' (length=254)
  public 'post_title' => string '2014-06-18-08-05-20 - Jack Coldrick' (length=35)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'open' (length=4)

我希望我的用户能够编辑他们的订单,因此我目前正在做的是获取上述对象中order_content信息的引用。

string 'Order Code: 2014-06-18-08-05-20
Customer Name: Jack Coldrick
Customer Email: jack@xxx.ie
Next Day Delivery: yes
Order Items: 
Stock Code: G992/75BLK Quantity: 10
Stock Code: NETT FILM JMC Quantity: 6
Stock Code: WW42 Quantity: 4
Comments: Test Comment 123
' (length=254)

我基本上需要在String中存储此variables的每一部分,并使用这些variables填充编辑订单表单。我的问题是,以高效和有效的方式分解这个字符串的最佳方法是什么?例如,要获得我的客户名称,我最好是喜欢做类似

的事情
`$customerName = ???`
`echo $customerName;` //returns Jack Coldrick

1 个答案:

答案 0 :(得分:0)

您向我们展示的是\WP_Post对象,该对象已被拆分为单个属性。 WordPress的帖子也有Revision system。您只需在wp-config.php中启用它们。

define( 'WP_POST_REVISIONS', 3 );

每次更新(自定义)帖子类型时,WordPress都会使用旧版本并将其作为{$prefix}_posts表中的新行保存为revision。您还有一个用于区分版本和回滚到旧版本的UI。