PHP 从 iframe/对象嵌入数组中提取 YouTube 视频 ID?

发布于 2024-12-18 01:55:43 字数 1401 浏览 1 评论 0原文

我有一个 YouTube iframes/对象数组,如下所示:

[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>

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

我将如何/什么是最可靠的方法来提取每个视频的 URL(例如 vQSRNYgiuMk 或 jm1S43a-e3Y)?

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

[0] => "szL_PVuzWp0"
[1] => "jm1S43a-e3Y"
[2] => "7fTploFSbXA"
[3] => "vQSRNYgiuMk"

I have an array of youtube iframes/objects like so:

[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>

Note that the embed method can vary (usually <iframe>, occasionally <object>) (due to external data source).

How would I/what is the most reliable method to go about extracting the video URL (e.g. vQSRNYgiuMk or jm1S43a-e3Y) for each one?

Ultimately I want to end up with an array like so:

[0] => "szL_PVuzWp0"
[1] => "jm1S43a-e3Y"
[2] => "7fTploFSbXA"
[3] => "vQSRNYgiuMk"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2024-12-25 01:55:43

不要使用正则表达式:

   $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
                  } 
               } 
            } 
         } 
      }
   }

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

Don't use regex please :

   $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
                  } 
               } 
            } 
         } 
      }
   }

Not a complete solution. But you get the point...

誰ツ都不明白 2024-12-25 01:55:43
foreach($arr as $i=>$a){
    $start = strpos($a, "/v/") + 3;
    if(!$start) $start = strpos($a, "/embed/") + 7;
    $qm = strpos("?");
    $length = $qm - $start;
    $new_array[$i] = substr($a, $start, $length);
}
foreach($arr as $i=>$a){
    $start = strpos($a, "/v/") + 3;
    if(!$start) $start = strpos($a, "/embed/") + 7;
    $qm = strpos("?");
    $length = $qm - $start;
    $new_array[$i] = substr($a, $start, $length);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文