将 iPhone 应用程序交易收据信息解码为 PHP 数组

发布于 2024-12-10 07:19:43 字数 162 浏览 0 评论 0原文

{ “签名” = “Alof/CEJ8a==”; “购买信息”=“ewoJIml0”; “环境”=“测试”; “豆荚”=“100”; “签名状态”=“0”; }

将 iPhone 应用内购买交易帖子解析为数组的最佳方法是什么?谢谢。

{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0";
"environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }

What is the best way with PHP to parse this iPhone in-app purchase transaction post into an array? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-12-17 07:19:43

不确定这是“最好”的方法,但这是一种方法。由于数据字符串由公共字符分隔(每个键/值由 ; 设置,然后键/值之间有 =),因此大部分解析可以是使用 explode() 完成。您可以使用 trim() - 使用自定义字符列表 - 删除整个字符串以及键/值周围的额外字符。

请注意,这将期望键永远不会有 = (尽管值可能):

$data = '{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0"; "environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }';

//remove the braces, spaces, and trailing semicolon
$data = trim($data, '{}; ');

$array = array();

//split on ';'
foreach(explode(';', $data) as $set){
  //parse key/value
  list($key, $value) = explode('=', $set, 2);
  $array[trim($key, '"\' ')] = trim($value, '"\' ');
}

print_r($array);

输出:

Array
(
  [signature] => Alof/CEJ8a==
  [purchase-info] => ewoJIml0
  [environment] => Test
  [pod] => 100
  [signing-status] => 0
)

Not sure this is the 'best' way, but it's one way. Since the data string is separated by common characters (each key/value set by a ;, then an = between the key/value), the bulk of the parsing can be done with explode(). And you can use trim() - with a custom character list - to remove the extra characters around the whole string, and the keys/values.

Note that this will expects that a key never has a = (although a value may):

$data = '{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0"; "environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }';

//remove the braces, spaces, and trailing semicolon
$data = trim($data, '{}; ');

$array = array();

//split on ';'
foreach(explode(';', $data) as $set){
  //parse key/value
  list($key, $value) = explode('=', $set, 2);
  $array[trim($key, '"\' ')] = trim($value, '"\' ');
}

print_r($array);

Output:

Array
(
  [signature] => Alof/CEJ8a==
  [purchase-info] => ewoJIml0
  [environment] => Test
  [pod] => 100
  [signing-status] => 0
)
呢古 2024-12-17 07:19:43

您可以将 = 替换为 : 并将 ; 替换为 , ,然后使用 json_decode()。

像这样:

$data = '{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0"; "environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }';
$data = explode('" =',$data);
$data = implode('" :',$data);
$data = explode(';',$data);
array_pop($data);
$data = implode(',',$data);
$data = explode(' ',$data);
$data = implode('',$data);
$data .= "}";
$data = json_decode($data, true);

查看此处

You could replace the ='s with :'s and the ;'s with ,'s and then use json_decode().

Like so:

$data = '{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0"; "environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }';
$data = explode('" =',$data);
$data = implode('" :',$data);
$data = explode(';',$data);
array_pop($data);
$data = implode(',',$data);
$data = explode(' ',$data);
$data = implode('',$data);
$data .= "}";
$data = json_decode($data, true);

Check it out here.

夏至、离别 2024-12-17 07:19:43
$data = '{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0"; "environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }';
$search = array('/\s\=\s/', '/;\s\}$/', '/;/');
$replace = array(':', '}', ',');
$json = preg_replace($search, $replace, $data);

print_r(json_decode($json));

输出:

Array
(
    [signature] => Alof/CEJ8a==
    [purchase-info] => ewoJIml0
    [environment] => Test
    [pod] => 100
    [signing-status] => 0
)
$data = '{ "signature" = "Alof/CEJ8a=="; "purchase-info" = "ewoJIml0"; "environment" = "Test"; "pod" = "100"; "signing-status" = "0"; }';
$search = array('/\s\=\s/', '/;\s\}$/', '/;/');
$replace = array(':', '}', ',');
$json = preg_replace($search, $replace, $data);

print_r(json_decode($json));

Output:

Array
(
    [signature] => Alof/CEJ8a==
    [purchase-info] => ewoJIml0
    [environment] => Test
    [pod] => 100
    [signing-status] => 0
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文