如何在源页面上获取mp4网址?

时间:2019-05-28 01:53:53

标签: php regex string file-get-contents preg-match-all

我的页面来源具有以下html(js):

<script>
playlist.sources.push({
    label: "480p",
    source: "//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e",
    index: 1
});</script>


我想得到:

//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e

我的代码:

<?php
  // Check if the URL parameter for our proxy is set.
  if (!empty($_GET['url'])) {
    if (filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
      $grabs = file_get_contents("{$_GET['url']}");


      $grab = json_decode($grabs, true);
// I don't know what to do next

      echo "{$video}" />";
    } else {
      echo "Given URL is not valid.";
    }
  } else {
    echo "You need to specify the URL.";
  }
?>

并显示在内容页面中。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

在这里,我们可以尝试使用带有简单表达式的preg_match_all

source: "(.+)",

我们所需的输出保存在捕获组$1中。

测试

$re = '/source: "(.+)",/m';
$str = '<script>
playlist.sources.push({
    label: "480p",
    source: "//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e",
    index: 1
});</script>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches[0][1]);

DEMO


我不太确定此file_get_content是否可以工作:

$str = file_get_contents('https://tv.zing.vn/video/id/IWZBFE8U.html?t=388');
$str = mb_convert_encoding($str, 'HTML-ENTITIES', "UTF-8");
var_dump($str);