从使用 dom php 给出 URL 中获取元素



用php构建一个MTDB DB,需要从URL中抓取一个特定的标签。

要从网址获取的标记

vars.disqus = '';
vars.lists = [];
vars.titleId = '35079';
vars.trailersPlayer = 'default';
vars.userId = '907791';
vars.title = {"id":35079,"title":"Family Vacation","trailer":35097.flv,"timing":0.50sec}

我需要

"id":35079,"title":"Family Vacation","trailer":35097.flv,"timing":0.50sec

我的代码:

$html = 'myurl';
libxml_use_internal_errors(TRUE); $dom = new DOMDocument; $dom->loadHTMLFile($html); libxml_clear_errors();
$xp = new DOMXpath($dom); $nodes = $xp->query('//script[@'id','trailer','title');
echo $nodes->item(0)->nodeValue;
  1. "Tag" 不是 HTML 格式,它看起来像一些 javascript 代码~~

  2. 要解析这些字符串,只需通过正则表达式

    preg_match('/titles*=s*{([^}]+)}/', $str, $matches);
    var_dump($matches[1]);
    

最新更新