Php preg_match_all из документа JSON

Здравствуйте, я хотел бы использовать preg_match_all в PHP для анализа «{» id «: xxxxxxxxxx,» оппонент «» из следующего из документа json.
Я хотел бы получить следующие идентификаторы: 0:
3043248027
3041643291
3043251372
3041727642
3042908044
3041713366

    preg_match_all('/{"id":(.*),"opponent"/', $jogo, $matchesg);
foreach($matchesg[1] as $jog):
echo "<br><br>" . "jogos: " . $jog . "<br>";
endforeach;{"time":"04/21/2015 11:35:19 EST","coins":87,"extra_shots":2,"lives":{"quantity":2,"max":3,"unlimited":false,"next_increment":2039},"inbox":{"total":0,"news":0},"unread_conversations":0,"level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false},"list":[{"id":3043248027,"opponent":{"id":170316372,"alerts_count":0,"username":"vanessa_17_maia","facebook_id":"100003756619283","facebook_name":"Vanessa Maia","fb_show_picture":true,"fb_show_name":true,"allow_og_posts":true,"level_data":{"level":15},"is_friend":true},"game_status":"PENDING_APPROVAL","language":"PT","created":"04/21/2015 09:59:40 EST","last_turn":"04/21/2015 09:59:40 EST","type":"NORMAL","expiration_date":"04/24/2015 09:59:40 EST","my_turn":true,"statistics":{"player_one_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0},"player_two_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0}},"duelGameType":false,"normalType":true,"spins_data":{"spins":[{"type":"NORMAL","questions":[{"question":{"id":16934778,"category":"SCIENCE","text":"Como se divide o sistema nervoso humano?","answers":["Sistema Nervoso Central E Periférico","Sistema Nervoso Primário E Secundário","Encéfalo E Medula Espinhal","Córtex E Medula Espinhal"],"author":{"id":54338805,"name":"Ana Lúcia","username":"ana_l9216","facebook_id":"100002637207623","facebook_name":"Ana Lúcia","fb_show_picture":true,"fb_show_name":true},"correct_answer":0,"media_type":"NORMAL"},"powerup_question":{"id":50299,"category":"SCIENCE","text":"Quantas cavidades o coracao tem?","answers":["5","1","7","4"],"author":{"id":23760737,"name":"Andreia Cardoso","username":"andreia.cardoso.3950178","facebook_id":"100000947372543","facebook_name":"Andreia Cardoso","fb_show_picture":true,"fb_show_name":true},"correct_answer":3,"media_type":"NORMAL"}}]}]},"available_crowns":["ENTERTAINMENT","SPORTS","ARTS","HISTORY","SCIENCE","GEOGRAPHY"],"my_player_number":1,"available_extra_shots":1,"player_one":{"charges":0},"player_two":{"charges":0},"round_number":1,"sub_status":"P1_WAITING_FIRST_TURN","is_random":false,"unread_messages":0,"status_version":0,"my_level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false}},{"id":3041643291,"opponent":{"id":170316372,"alerts_count":0,"username":"vanessa_17_maia","facebook_id":"100003756619283","facebook_name":"Vanessa Maia","fb_show_picture":true,"fb_show_name":true,"allow_og_posts":true,"level_data":{"level":15},"is_friend":true},"game_status":"PENDING_APPROVAL","language":"PT","created":"04/21/2015 05:22:29 EST","last_turn":"04/21/2015 05:22:29 EST","type":"NORMAL","expiration_date":"04/24/2015 05:22:29 EST","my_turn":true,"statistics":{"player_one_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0},"player_two_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0}},"duelGameType":false,"normalType":true,"spins_data":{"spins":[{"type":"NORMAL","worst":true,"performance":72,"questions":[{"question":{"id":17060583,"category":"ARTS","text":"Como se chama a técnica que consiste em pintar pontos muito pequenos para que de longe pareça uma pintura?","answers":["Pontilhismo","Pontualização","Pontuação","Pontolição"],"author":{"id":52569793,"name":"Joaquin D'Angelo","username":"joaquin.dangelo.969","facebook_id":"100006350769699","facebook_name":"Joaquin D'Angelo","fb_show_picture":true,"fb_show_name":true},"translator":{"id":99642564,"name":"risoseusorriso","username":"risoseusorriso","facebook_id":"100001863319561","facebook_name":"Risocleide Matos","fb_show_picture":false,"fb_show_name":false},"correct_answer":0,"media_type":"NORMAL"},"powerup_question":{"id":279029,"category":"ARTS","text":"Como o filósofo Sócrates morreu?","answers":["Enforcado","Envenenado","Assassinato","Queimado"],"author":{"id":25463644,"name":"Luiza Arida Carvalho","username":"luiza.aridacarvalho","facebook_id":"100000584034159","facebook_name":"Luiza Arida Carvalho","fb_show_picture":true,"fb_show_name":true},"correct_answer":1,"media_type":"NORMAL"}}]}]},"available_crowns":["ENTERTAINMENT","SPORTS","ARTS","HISTORY","SCIENCE","GEOGRAPHY"],"my_player_number":1,"available_extra_shots":1,"player_one":{"charges":0},"player_two":{"charges":0},"round_number":1,"sub_status":"P1_WAITING_FIRST_TURN","is_random":false,"unread_messages":0,"status_version":0,"my_level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false}},{"id":3043251372,"opponent":{"id":170316372,"alerts_count":0,"username":"vanessa_17_maia","facebook_id":"100003756619283","facebook_name":"Vanessa Maia","fb_show_picture":true,"fb_show_name":true,"allow_og_posts":true,"level_data":{"level":15},"is_friend":true},"game_status":"PENDING_APPROVAL","language":"PT","created":"04/21/2015 10:00:10 EST","last_turn":"04/21/2015 10:00:10 EST","type":"NORMAL","expiration_date":"04/24/2015 10:00:10 EST","my_turn":true,"statistics":{"player_one_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0},"player_two_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0}},"duelGameType":false,"normalType":true,"spins_data":{"spins":[{"type":"NORMAL","questions":[{"question":{"id":6622584,"category":"ENTERTAINMENT","text":"Quem foi Ray Charles?","answers":["Actor","Cantor","Pintor","Filósofo"],"author":{"id":47583412,"name":"Rafael Cassiano","username":"rafael.cassiano","facebook_id":"100000560878573","facebook_name":"Rafael Cassiano","fb_show_picture":true,"fb_show_name":true},"correct_answer":1,"media_type":"NORMAL"},"powerup_question":{"id":214441,"category":"ENTERTAINMENT","text":"Qual cantora ganhou um Oscar pela canção \"Skyfall\" do filme 007?","answers":["Adele","Madonna","Whitney Houston","Mariah Carey"],"author":{"id":24757604,"name":"Rodrigo Resque","username":"rodrigo.resque.3","facebook_id":"100000040931937","facebook_name":"Rodrigo Resque","fb_show_picture":true,"fb_show_name":true},"correct_answer":0,"media_type":"NORMAL"}}]}]},"available_crowns":["ENTERTAINMENT","SPORTS","ARTS","HISTORY","SCIENCE","GEOGRAPHY"],"my_player_number":1,"available_extra_shots":1,"player_one":{"charges":0},"player_two":{"charges":0},"round_number":1,"sub_status":"P1_WAITING_FIRST_TURN","is_random":false,"unread_messages":0,"status_version":0,"my_level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false}},{"id":3041727642,"opponent":{"id":170316372,"alerts_count":0,"username":"vanessa_17_maia","facebook_id":"100003756619283","facebook_name":"Vanessa Maia","fb_show_picture":true,"fb_show_name":true,"allow_og_posts":true,"level_data":{"level":15},"is_friend":true},"game_status":"PENDING_APPROVAL","language":"PT","created":"04/21/2015 05:44:04 EST","last_turn":"04/21/2015 05:44:04 EST","type":"NORMAL","expiration_date":"04/24/2015 05:44:04 EST","my_turn":true,"statistics":{"player_one_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0},"player_two_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0}},"duelGameType":false,"normalType":true,"spins_data":{"spins":[{"type":"NORMAL","questions":[{"question":{"id":209748,"category":"SCIENCE","text":"Qual ação realizada pelas plantas para se alimentarem?","answers":["Fotossintese","Quimiossintese","Autótrofa","Heterótrofa"],"author":{"id":25277480,"name":"Alvaro Louzi","username":"alvaro.louzi","facebook_id":"100002612107535","facebook_name":"Alvaro Louzi","fb_show_picture":true,"fb_show_name":true},"correct_answer":0,"media_type":"NORMAL"},"powerup_question":{"id":16783119,"category":"SCIENCE","text":"Quais dos seguintes é um componente de uma pasta de dentes?","answers":["Cloreto se sódio","Fluoreto de sódio","Sulfato de amoniaco","Nitrato de magnésio"],"author":{"id":77605148,"name":"mrntc96","username":"mrntc96","fb_show_picture":false,"fb_show_name":false},"correct_answer":1,"media_type":"NORMAL"}}]}]},"available_crowns":["ENTERTAINMENT","SPORTS","ARTS","HISTORY","SCIENCE","GEOGRAPHY"],"my_player_number":1,"available_extra_shots":1,"player_one":{"charges":0},"player_two":{"charges":0},"round_number":1,"sub_status":"P1_WAITING_FIRST_TURN","is_random":false,"unread_messages":0,"status_version":0,"my_level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false}},{"id":3042908044,"opponent":{"id":170316372,"alerts_count":0,"username":"vanessa_17_maia","facebook_id":"100003756619283","facebook_name":"Vanessa Maia","fb_show_picture":true,"fb_show_name":true,"allow_og_posts":true,"level_data":{"level":15},"is_friend":true},"game_status":"PENDING_APPROVAL","language":"PT","created":"04/21/2015 09:09:18 EST","last_turn":"04/21/2015 09:09:18 EST","type":"NORMAL","expiration_date":"04/24/2015 09:09:18 EST","my_turn":true,"statistics":{"player_one_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0},"player_two_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0}},"duelGameType":false,"normalType":true,"spins_data":{"spins":[{"type":"NORMAL","questions":[{"question":{"id":202961,"category":"SCIENCE","text":"Qual é o principal elemento químico presente no petróleo?","answers":["Hidrogênio","Bário","Petruleum","Carbono"],"author":{"id":23913646,"name":"jadym","username":"jadym","fb_show_picture":false,"fb_show_name":false},"correct_answer":3,"media_type":"NORMAL"},"powerup_question":{"id":14986854,"category":"SCIENCE","text":"O que é a anona?","answers":["Uma flor","Uma planta","Uma nona parte de uma medida","Fruto"],"author":{"id":69278688,"name":"Débora Oliveira","username":"debora.pereira.739326","facebook_id":"100002291948861","facebook_name":"Débora Oliveira","fb_show_picture":true,"fb_show_name":true},"correct_answer":3,"media_type":"NORMAL"}}]}]},"available_crowns":["ENTERTAINMENT","SPORTS","ARTS","HISTORY","SCIENCE","GEOGRAPHY"],"my_player_number":1,"available_extra_shots":1,"player_one":{"charges":0},"player_two":{"charges":0},"round_number":1,"sub_status":"P1_WAITING_FIRST_TURN","is_random":false,"unread_messages":0,"status_version":0,"my_level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false}},{"id":3041713366,"opponent":{"id":170316372,"alerts_count":0,"username":"vanessa_17_maia","facebook_id":"100003756619283","facebook_name":"Vanessa Maia","fb_show_picture":true,"fb_show_name":true,"allow_og_posts":true,"level_data":{"level":15},"is_friend":true},"game_status":"PENDING_APPROVAL","language":"PT","created":"04/21/2015 05:40:40 EST","last_turn":"04/21/2015 05:40:40 EST","type":"NORMAL","expiration_date":"04/24/2015 05:40:40 EST","my_turn":true,"statistics":{"player_one_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0},"player_two_statistics":{"correct_answers":0,"incorrect_answers":0,"challenges_won":0,"questions_answered":0,"crowns_won":0}},"duelGameType":false,"normalType":true,"spins_data":{"spins":[{"type":"NORMAL","questions":[{"question":{"id":23917079,"category":"ENTERTAINMENT","text":"Qual foi o ator que interpretou capitão América no filme \"The Avengers\"?","answers":["Brad Pitt","Chris Evans","Hugh Jackman","Leonardo Di Caprio"],"author":{"id":108070729,"name":"Ruben Silva","username":"rubenexsivex","facebook_id":"100001734817106","facebook_name":"Ruben Silva","fb_show_picture":true,"fb_show_name":true},"correct_answer":1,"media_type":"NORMAL"},"powerup_question":{"id":43384354,"category":"ENTERTAINMENT","text":"Em qual dos filmes tem uma pesonagem chamada Jasmine?","answers":["Alladin","Pocahontas 2","Rei leão","Carros"],"author":{"id":167223554,"name":"inesmartinsferraz","username":"inesmartinsferraz","fb_show_picture":false,"fb_show_name":false},"correct_answer":0,"media_type":"NORMAL"}}]}]},"available_crowns":["ENTERTAINMENT","SPORTS","ARTS","HISTORY","SCIENCE","GEOGRAPHY"],"my_player_number":1,"available_extra_shots":1,"player_one":{"charges":0},"player_two":{"charges":0},"round_number":1,"sub_status":"P1_WAITING_FIRST_TURN","is_random":false,"unread_messages":0,"status_version":0,"my_level_data":{"level":31,"points":553,"progress":84,"goal_points":558,"level_up":false}}],"app_config":{"fp_friends_ttl":300,"fp_chats_ttl":300,"version":1429634119,"lives":{"increment_interval":3600,"increment_quantity":1},"questions_time":30,"power_ups":[{"name":"DOUBLE_CHANCE","cost":5},{"name":"BOMB","cost":5},{"name":"SWAP_QUESTION","cost":3},{"name":"EXTRA_TIME","cost":1},{"name":"SECOND_CHANCE","cost":15}],"crowns_per_turn_limit":3,"final_duel_rounds":25,"available_languages":["ES","EN","PT","FR","IT","CA","DE","RU","JA","NL"],"extra_shots_limit":1,"rejected_questions_expiration":7,"dashboard_ttl":300,"duel_games_enabled":false,"duel_games_players_min":2,"duel_games_players_max":30,"show_questions_reload_seconds":1,"codes_enabled":false},"notification_id":"","country":"PT","new_achievements":false,"country_confirmation":true,"see_ranking":false}

Спасибо.

-2

Решение

Может быть, регулярное выражение — не лучший выбор, но опять же, выбор имеет значение. Вы можете легко получить нужные значения, специально захватывая цифры с помощью группы захвата с (\d+), С помощью (.*) по сути собирается захватить все, что вы определенно не хотите.

$re = "/\"id\":(\\d+),\"opponent\"/m";
preg_match_all($re, $str, $matches);

Пример:

https://regex101.com/r/aK6iP2/1

2

Другие решения

Не используйте preg_match, предпочтительнее декодировать JSON, а затем искать ключ:

$json = json_decode('...', true);
$lists = $json['list'];
foreach($lists as $list) {
echo "<br><br>" . "jogos: " . $list['id']. "<br>";
}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector