21,887
社区成员
发帖
与我相关
我的任务
分享
$html = 'id=007&enc=Hg27sDS7&data=ISJsf2UF{"bind_card_list":[{"type":"1","code":"CCB","name":"建设银行","id":"54321","card_last":"8301","card_top":"621424","phone":"13298363188"},{"type":"0","code":"CCB","name":"建设银行","id":"12345","phone":"13224422188"}],"member_id":"12345","merchant_id":"0"}';
我想取出里面的json,请教一下改如何操作,如果正则的话该如何写匹配,谢谢各位了!!!
$html = 'id=007&enc=Hg27sDS7&data=ISJsf2UF{"bind_card_list":[{"type":"1","code":"CCB","name":"建设银行","id":"54321","card_last":"8301","card_top":"621424","phone":"13298363188"},{"type":"0","code":"CCB","name":"建设银行","id":"12345","phone":"13224422188"}],"member_id":"12345","merchant_id":"0"}';
preg_match('/\{(.*)\}/s', $html, $matches);
$data = json_decode($matches[0], true);
print_r($data);
$html = 'id=007&enc=Hg27sDS7&data=ISJsf2UF{"bind_card_list":[{"type":"1","code":"CCB","name":"建设银行","id":"54321","card_last":"8301","card_top":"621424","phone":"13298363188"},{"type":"0","code":"CCB","name":"建设银行","id":"12345","phone":"13224422188"}],"member_id":"12345","merchant_id":"0"}';
parse_str($html, $a);
echo strstr($a['data'], '{');
$html = 'id=007&enc=Hg27sDS7&data=ISJsf2UF{"bind_card_list":[{"type":"1","code":"CCB","name":"建设银行","id":"54321","card_last":"8301","card_top":"621424","phone":"13298363188"},{"type":"0","code":"CCB","name":"建设银行","id":"12345","phone":"13224422188"}],"member_id":"12345","merchant_id":"0"}';
preg_match('/\{.+\}/s', $html, $m);
echo $m[0];
$data = json_decode(strstr($html, '{'), true);
或者用explode和substr分割
$html = 'id=007&enc=Hg27sDS7&data=ISJsf2UF{"bind_card_list":[{"type":"1","code":"CCB","name":"建设银行","id":"54321","card_last":"8301","card_top":"621424","phone":"13298363188"},{"type":"0","code":"CCB","name":"建设银行","id":"12345","phone":"13224422188"}],"member_id":"12345","merchant_id":"0"}';
parse_str($html,$arr);
echo substr($arr['data'],strpos($arr['data'],'{'));
<?php
$a = 'id=007&enc=Hg27sDS7&data=ISJsf2UF{"bind_card_list":[{"type":"1","code":"CCB","name":"建设银行","id":"54321","card_last":"8301","card_top":"621424","phone":"13298363188"},{"type":"0","code":"CCB","name":"建设银行","id":"12345","phone":"13224422188"}],"member_id":"12345","merchant_id":"0"}';
preg_match("/{.+}/", $a,$b);
$c = json_decode($b[0],1);
print_r($c);
Array
(
[bind_card_list] => Array
(
[0] => Array
(
[type] => 1
[code] => CCB
[name] => 建设银行
[id] => 54321
[card_last] => 8301
[card_top] => 621424
[phone] => 13298363188
)
[1] => Array
(
[type] => 0
[code] => CCB
[name] => 建设银行
[id] => 12345
[phone] => 13224422188
)
)
[member_id] => 12345
[merchant_id] => 0
)