php preg_match从字符串中找到困难的JSON

时间:2016-01-25 08:50:20

标签: php regex preg-match

原文是:

;\n}\nvar vars = {\"uid\":\"170785079\",\"vid\":171461352,\"oid\":170785079,\"host\":509202,\"vtag\":\"493bf2f9dd\",\"ltag\":\"l_8f6ca452\",\"vkid\":171461352,\"md_title\":\"6dfdbda80019208839183a713f243a41\",\"md_author\":\"Ayxan Əmiraslanl\",\"author_id\":170785079,\"author_href\":\"\\\/mr.ayxan\",\"hd\":3,\"no_flv\":1,\"hd_def\":-1,\"dbg_on\":0,\"t\":\"\",\"duration\":244,\"angle\":0.000000,\"img_angle\":0.000000,\"repeat\":0,\"show_ads_preroll\":0,\"show_ads_postroll\":0,\"show_ads_promo_preroll\":0,\"show_ads_overlay\":0,\"ads_type\":-1,\"legal_owner\":0,\"eid1\":0,\"slot\":0,\"g\":1,\"a\":18,\"puid34\":0,\"water_mark\":\"\",\"can_rotate\":1,\"hash\":\"19e3bc2d2ae2f2ce34f6fbaefcf93e2d\",\"hash2\":\"246f9680b9b4f835\",\"is_vk\":\"1\",\"is_ext\":\"0\",\"referrer\":\"\",\"c3\":\"\",\"sample_id\":-1,\"cat_id\":13,\"sitezone\":17,\"ads_preview\":0,\"puid4\":0,\"puid5\":14,\"puid6\":86,\"pl_type\":\"other\",\"nolikes\":1,\"lang_add\":\"Add to My Videos\",\"lang_added\":\"Video added to My Videos\",\"lang_share\":\"Share\",\"lang_like\":\"Like\",\"lang_subscribe\":\"Subscribe\",\"lang_subscribed\":\"You have subscribed\",\"lang_volume_on\":\"Unmute\",\"lang_volume_off\":\"Mute\",\"lang_volume\":\"Volume\",\"lang_hdsd\":\"Change Video Quality\",\"lang_quality_auto\":\"Auto\",\"lang_open_popup\":\"Expand\",\"lang_fullscreen\":\"Full Screen\",\"lang_window\":\"Minimize\",\"lang_rotate\":\"Rotate\",\"lang_ads_link\":\"Advertiser's Site\",\"lang_ads\":\"Ads\",\"lang_ads_skip\":\"Skip ad\",\"lang_ads_skip_time\":\"Skip ads in {time} s\",\"lang_report_problem\":\"Report a problem..\",\"lang_replay\":\"Replay\",\"lang_next_cancel\":\"Cancel\",\"video_play_hd\":\"Watch in HD\",\"video_stop_loading\":\"Stop Download\",\"video_player_version\":\"VK Video Player\",\"goto_orig_video\":\"Go to Video\",\"video_get_video_code\":\"Copy video code\",\"video_load_error\":\"The video has not uploaded yet or the server is not available\",\"video_get_current_url\":\"Copy frame link\",\"lang_next\":\"Next video\",\"url240\":\"https:\\\/\\\/cs509202.vk.me\\\/8\\\/u170785079\\\/videos\\\/493bf2f9dd.240.mp4?extra=SatA0TNoxI3a5jCGpYqhL9FIoS9kjnfIqvkVswFVnFDqJToNiAidxUb2ELSSrSEBNGEvU9Fiseuq5uEQSS_-afULYhnmxc6_vvsCA0mWSYbxDux-HA\",\"url360\":\"https:\\\/\\\/cs509202.vk.me\\\/8\\\/u170785079\\\/videos\\\/493bf2f9dd.360.mp4?extra=SatA0TNoxI3a5jCGpYqhL9FIoS9kjnfIqvkVswFVnFDqJToNiAidxUb2ELSSrSEBNGEvU9Fiseuq5uEQSS_-afULYhnmxc6_vvsCA0mWSYbxDux-HA\",\"url480\":\"https:\\\/\\\/cs509209.vk.me\\\/8\\\/u170785079\\\/videos\\\/493bf2f9dd.480.mp4?extra=SatA0TNoxI3a5jCGpYqhL9FIoS9kjnfIqvkVswFVnFDqJToNiAidxUb2ELSSrSEBNGEvU9Fiseuq5uEQSS_-afULYhnmxc6_vvsCA0mWSYbxDux-HA\",\"url720\":\"https:\\\/\\\/cs509209.vk.me\\\/8\\\/u170785079\\\/videos\\\/493bf2f9dd.720.mp4?extra=SatA0TNoxI3a5jCGpYqhL9FIoS9kjnfIqvkVswFVnFDqJToNiAidxUb2ELSSrSEBNGEvU9Fiseuq5uEQSS_-afULYhnmxc6_vvsCA0mWSYbxDux-HA\",\"jpg\":\"https:\\\/\\\/pp.vk.me\\\/c633320\\\/v633320079\\\/10dcc\\\/GDFulD47LS8.jpg\",\"timeline_thumbs\":1,\"timeline_thumbs_jpg\":\"https:\\\/\\\/pp.vk.me\\\/c628028\\\/v628028079\\\/363df\\\/FVHRrSkD6HM.jpg,https:\\\/\\\/pp.vk.me\\\/c633322\\\/v633322079\\\/f06a\\\/CjskgJb-xxU.jpg\",\"timeline_thumbs_per_row\":10,\"timeline_thumbs_per_image\":100,\"timeline_thumbs_total\":122,\"timeline_thumb_width\":133.000000,\"timeline_thumb_height\":75,\"ip_subm\":1,\"proxy\":\"psv4\",\"https\":1,\"viewer_id\":170785079,\"nologo\":1,\"show_next\":0,\"show_suggestions\":0,\"liked\":0,\"add_hash\":\"0685c16b426a731323\",\"added\":0,\"can_add\":0,\"no_title\":0,\"vsegs_size\":24.000000,\"vsegs_hash\":\"1433edfd589e9d072b\",\"module\":\"direct\",\"playlist_id\":null};\n\nif (!var

如何在 var vars \ n \ n nif之间获取字符串(!var

我试过这段代码:

preg_match('/var vars = (.*?);\n\nif (!var/mis', $get, $result)

3 个答案:

答案 0 :(得分:1)

具有正向前瞻和懒惰量词(根据您的评论修改):

<?php
$file = file_get_contents("http://shopnow.az/text.txt");
$regex = '~var vars = ({(.*?)(?=};)};)~';
preg_match_all($regex, $file, $matches);
$code = $matches[1][0];
echo $code;
?>

a demo on regex101.com。这可行,但它非常慢且容易出错。是否有其他方法可以获得有问题的代码?

答案 1 :(得分:0)

只需使用lookbehind和lookahead:

preg_match_all('/(?<=var\svars).*(?=\\n\\nif \(!var)/', $get, $matches);
var_dump($matches[0][0]);

Demo

演示PHP Sandbox:

http://sandbox.onlinephpfunctions.com/code/dd38a3bb4384625da11ffaa64795a4ab7b5fbb17

答案 2 :(得分:0)

这是一种获取内容而无需使用外观的简单方法:

preg_match('~var vars = (\{.*?\};)~m', $get, $json_matches);
$json_match = $json_matches[1];
print $json_match;

我几乎只是将大括号之间的所有数据从一个匹配到另一个。

这是一个有效的演示:

http://ideone.com/ta4sHw