PHP从iframe/object嵌入数组中提取youtube视频ID?

sgb*_*sgb 3 php regex youtube dom

我有一组youtube iframes/objects如下:

[0] => <iframe width="600" height="338" src="http://www.youtube.com/embed/szL_PVuzWp0?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>
[1] => <object width="600" height="338"><param name="movie" value="http://www.youtube.com/v/jm1S43a-e3Y?version=3&feature=oembed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jm1S43a-e3Y?version=3&feature=oembed" type="application/x-shockwave-flash" width="600" height="338" allowscriptaccess="always" allowfullscreen="true"></embed></object>
[2] => <iframe width="600" height="338" src="http://www.youtube.com/embed/7fTploFSbXA?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>
[3] => <iframe width="600" height="338" src="http://www.youtube.com/embed/vQSRNYgiuMk?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)

请注意,嵌入方法可能会有所不同(通常<iframe>偶尔<object>)(由于外部数据源).

如何将I /什么是最可靠的方法去为每一个提取视频网址(如vQSRNYgiuMk或jm1S43a-e3Y)?

最终我想最终得到一个像这样的数组:

[0] => "szL_PVuzWp0"
[1] => "jm1S43a-e3Y"
[2] => "7fTploFSbXA"
[3] => "vQSRNYgiuMk"
Run Code Online (Sandbox Code Playgroud)

Fai*_*Dev 5

请不要使用正则表达式:

   $dom_document = new DOMDocument();

   $dom_document->loadHTML($html);

   //use DOMXpath to navigate the html with the DOM
   $dom_xpath = new DOMXpath($dom_document);

   // if you want to get the all the iframes
   $iframes = $dom_xpath->query("//iframe");

   if (!is_null($iframes)) {
      foreach ($iframes as $iframe) {
        if($iframe->hasAttributes()){ 
            $attributes = $iframe->attributes; 
            if(!is_null($attributes)){ 
               foreach ($attributes as $index=>$attr){ 
                  if($attr->name == 'src'){ 
                     $curSrc = $attr->value; 
                     //use regex here to extract what you want
                  } 
               } 
            } 
         } 
      }
   }
Run Code Online (Sandbox Code Playgroud)

完整的解决方案.但是你明白了......