如何preg匹配解析值addr:在此字符串中

时间:2014-04-06 19:10:21

标签: php regex

我收到了一串区块链,如下所示

{"double_spend":false,"block_height":294529,"time":1396809215,"inputs":[{"prev_out":{"n":1,"value":68000,"addr":"1ZZZZhhdLL5ZKeD9gbakpt4J3vzGUMVHg","tx_index":53988341,"type":0,"script":"76a91406287de03de2b4cdd4095ce75c4101004677876b88ac"},"script":"76a91406287de03de2b4cdd4095ce75c4101004677876b88ac"}],"vout_sz":2,"relayed_by":"107.170.67.24","hash":"5f63a2dcbef2e534934b1919430d2ccafa030ec1f8011581a58b43b1d529db5e","vin_sz":1,"tx_index":54014213,"ver":1,"out":[{"n":0,"value":10000,"addr":"1L3ZER3u5qg8ZZ59c3Td9ecUWekESzCVnF","tx_index":54014213,"spent":true,"type":0,"script":"76a914d0e6208c2d26e09ff8b51c1b5ba12b4dcf66b71f88ac"},{"n":1,"value":48000,"addr":"1ZZZZhhdLL5ZKeD9gbakpt4J3vzGUMVHg","tx_index":54014213,"spent":true,"type":0,"script":"76a91406287de03de2b4cdd4095ce75c4101004677876b88ac"}],"size":258}

如何将" addr":的值解析为数组

数组的print_r应该返回

 [0]  1ZZZZhhdLL5ZKeD9gbakpt4J3vzGUMVHg
 [1]  1L3ZER3u5qg8ZZ59c3Td9ecUWekESzCVnF
 [2]  1ZZZZhhdLL5ZKeD9gbakpt4J3vzGUMVHg

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用json_decode代替正则表达式,因为您的输入看起来像json

$arr = array();

$json = json_decode($text);  // $text is the input
foreach($json->inputs as $x){
    array_push($arr, $x->prev_out->addr);
}
foreach($json->out as $x){
    array_push($arr, $x->addr);
}

print_r($arr);