dqjcb132285 2016-01-25 08:50
浏览 101
已采纳

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

The original text is:

;
}
var 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};

if (!var

how can I get string between var vars and if (!var

I tried this code:

preg_match('/var vars = (.*?);

if (!var/mis', $get, $result)
  • 写回答

3条回答 默认 最新

  • duan1930 2016-01-25 15:47
    关注

    Here is a simple way to get your content without needing to use lookarounds:

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

    I am pretty much just matching all of the data between the curly braces from one to the other.

    Here is a working demo:

    http://ideone.com/ta4sHw

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化