[英]PHP how to separate a query string into variables
I have a paypal form that submits a the value of a custom hidden input. 我有一个Paypal表单,提交一个自定义隐藏输入的值。 Since I have a few extra pieces of data to parse, I've added them as a query string.
由于我还有一些额外的数据要解析,因此我将它们添加为查询字符串。 ie
即
<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. 然后,我有一个名为paypal-ipn.php的文件,该文件与paypal进行通信,并将付款数据添加到我的数据库中。 I get the values of each of the inputs using the $_POST method, ie
我使用$ _POST方法获得每个输入的值,即
$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, ie
我的问题是,如何将查询字符串分成单独的变量,即
$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? 有没有办法使用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'];
Use parse string 使用解析字符串
Parses str as if it were the query string passed via a URL and sets variables in the current scope.
将str解析为好像是通过URL传递的查询字符串,并在当前作用域中设置变量。
<?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 http://php.net/manual/zh/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;
You can do something like this: 您可以执行以下操作:
$string = "payment-type=deposit&user-id=1&user-name=admin";
parse_str($string, $output);
echo $output['payment-type'];
echo $output['user-id'];
echo $output['user-name'];
//Output
deposit1admin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.