使用SimpleHTMLDom从javascript var解析

时间:2018-09-17 11:00:30

标签: javascript php html curl simple-html-dom

我有这段代码可以输出带有curl的源URL的源页面!

$url = 'http://source-page.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // add this one, it seems to spawn redirect 301 header
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); // spoof
$output = curl_exec($ch);
curl_close($ch);

$html = str_get_html($output);

在$ output中,我有这个:

var flashvars = {

    "image_url":"http://path-to-image.com",
    "video_title":"This is video title",
    "videoUrl":"http://this-is-path-to-mp4.com"

}

我想回显videoUrl,我已经尝试过:

$videoUrl = $html->find('flashvars[0].videoUrl');
echo $videoUrl

给我空洞的结果。这样做有什么好的代码?

1 个答案:

答案 0 :(得分:0)

其他人建议使用正则表达式+ json_decode,然后将其删除。 这就是我要做的:

$output = <<<EOF
var flashvars = {

  "image_url":"http://path-to-image.com",
  "video_title":"This is video title",
  "videoUrl":"http://this-is-path-to-mp4.com"

}
EOF;

$str = preg_match('/var flashvars = (\{.*?\})/s', $output, $m);
$data = json_decode($m[1], true);
echo $data['videoUrl'];