这输出为: 然后我有一个名为 paypal-ipn.p-6ren">
gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:32 24 4
gpt4 key购买 nike

我有一个提交自定义隐藏输入值的 Paypal 表单。因为我有一些额外的数据要解析,所以我将它们添加为查询字符串。即

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

这输出为:

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

然后我有一个名为 paypal-ipn.php 的文件,它与 paypal 通信并将支付数据添加到我的数据库中。我使用 $_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'];

最后一个将(希望)返回我的查询字符串。我的问题是,如何将查询字符串分离成单独的变量,即

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

有没有办法使用 php 来做到这一点?

这是我根据以下答案尝试的最新代码:

$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'];

最佳答案

使用解析字符串

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

已更新

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;

关于PHP如何将查询字符串分隔成变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com