duanju8308 2015-12-01 23:39
浏览 42
已采纳

PHP如何将查询字符串分成变量

I have a paypal form that submits a the value of a custom hidden input. Since I have a few extra pieces of data to parse, I've added them as a query string. i.e.

<input type="hidden" name="custom" value="payment-type=deposit&user-id=<?php echo $user_id; ?>&user-name=<?php echo $user_name; ?>">

This outputs as:

<input type="hidden" name="custom" value="payment-type=deposit&user-id=1&user-name=admin">

I then have a file called paypal-ipn.php which communicates with paypal and also adds the payment data to my database. I get the values of each of the inputs using the $_POST method, i.e

$item_name        = $_POST['item_name']; // wedding name
$booking_id       = $_POST['item_number']; // booking id
$payment_status   = $_POST['payment_status']; // payment status
$payment_amount   = $_POST['mc_gross']; // amount?
$payment_currency = $_POST['mc_currency']; // currency
$txn_id           = $_POST['txn_id']; // transaction id
$receiver_email   = $_POST['receiver_email']; // reciever email i.e. your email
$payer_email      = $_POST['payer_email']; // payer email i.e. client email
$custom_variables = $_POST['custom'];

The last one will (hopefully) return my query string. My question is, how can I separate out my query string into separate variables, i.e.

$payment_type = PAYMENT TYPE FROM STRING
$user_id = USER ID FROM STRING
$user_name = USER NAME FROM STRING

Is there a way to do this using php?

Here is my latest code that I'm trying based on answers below:

$custom_variables = $_POST['custom'];

parse_str($custom_variables);
echo $payment_type;  
echo $user_id; 
echo $user_name; 

parse_str($str, $output);
$payment_type = $output['payment_type'];
$user_id = $output['user_id'];
$user_name = $output['user_name'];
  • 写回答

2条回答 默认 最新

  • doulang1945 2015-12-01 23:45
    关注

    Use parse string

    Parses str as if it were the query string passed via a URL and sets variables in the current scope.

    <?php
    $str = "first=value&arr[]=foo+bar&arr[]=baz";
    parse_str($str);
    echo $first;  // value
    echo $arr[0]; // foo bar
    echo $arr[1]; // baz
    
    parse_str($str, $output);
    echo $output['first'];  // value
    echo $output['arr'][0]; // foo bar
    echo $output['arr'][1]; // baz
    
    ?>
    

    http://php.net/manual/en/function.parse-str.php

    Updated

    parse_str($custom_variables , $output);
    $payment_type = $output['payment_type'];
    $user_id = $output['user_id'];
    $user_name = $output['user_name'];
    
    echo $payment_type;  
    echo $user_id; 
    echo $user_name; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看