PHP从iframe / object数组中提取youtube视频ID嵌入? [英] PHP extract youtube video IDs from array of iframe/object embeds?

查看:267
本文介绍了PHP从iframe / object数组中提取youtube视频ID嵌入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组youtube iframe /对象,如下所示:

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>

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

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

我/ /最可靠的方法是为每个人提取视频网址(例如vQSRNYgiuMk或jm1S43a-e3Y)?

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"


推荐答案

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);
}

这篇关于PHP从iframe / object数组中提取youtube视频ID嵌入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆