使用SimpleHTMLDOM在PHP中解析HTML iFrame SRC

时间:2017-04-30 15:26:22

标签: php html dom

这就是我的HTML的样子:

<div class="academy_page">
    <span class="academy_page_header">Academy:</span>
    <div>
        <div class="videopage_block" align="center">
            <iframe class="videopage_video" src="//player.vimeo.com/video/67353453644?title=0&amp;byline=0&amp;portrait=0" width="300" height="150" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
            <div class="academy_text">How to Ride a Skateboard</div>
        </div>
        <div class="videopage_block" align="center">
            <iframe class="videopage_video" src="//player.vimeo.com/video/9435345343?title=0&amp;byline=0&amp;portrait=0" width="300" height="150" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
            <div class="academy_text">How to Build a Skateboard</div>
        </div>
</div>

这就是我解析它的方式:

foreach($html->find('div.academy_page') as $element){

        foreach($element->find('div.videopage_block') as $dulce) {

            $item['title'] = $dulce->find('div.academy_text', 0)->plaintext;
            $item['vimeo_url'] = $dulce->find('iframe')->src;

            $returnArray[] = $item;
        }
     }

这是我在json中的结果:

[
  {
    "title": "How to Ride a Skateboard",
    "vimeo_url": null
  },
  {
    "title": "How to Build a Skateboard",
    "vimeo_url": null
  }
]

如何获取iFrame SRC?

我还试过$item['vimeo_url'] = $dulce->find('.videopage_video')->plaintext;而没有

2 个答案:

答案 0 :(得分:1)

我想你可以使用:

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//div[@class='videopage_block']") as $node){

    echo $node->getElementsByTagName('iframe')[0]->getAttribute("src");
    echo $node->getElementsByTagName('div')[0]->nodeValue;
}

<强>更新

获取视频ID:

$iframe_src = $node->getElementsByTagName('iframe')[0]->getAttribute("src")
$video_id = preg_replace('%.*?video/(\d+)\?.*%i', '$1', $iframe_src);

答案 1 :(得分:0)

您可以直接在SimpleHTMLDOM中指向src属性

  $iframe = $html->find( 'iframe' , 1 )->src ;

参考official manual