1

如何获取 youtube 和 vimeo 视频嵌入代码的视频 ID?youtube 视频嵌入代码如下所示:

<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>

其中的 ID 是 dbPmWbqAJPs。

vimeo 嵌入代码如下所示:

<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>

本例中的 ID 为 62468031。

我不想替换那些确切的 id,而是有一个 pregmatch/pregreplace 可以在 vimeo 和 youtube 的任何嵌入中找到 id。

4

2 回答 2

2

尝试这个

维密欧

$string='<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
preg_match_all( '~http://player.vimeo.com/video/(.*?)\?~si',$string,$M);
echo ($M[1][0]);

YouTube :

$string='<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>';
preg_match_all( '~http://www.youtube.com/embed/(.*?)\"~si',$string,$M);
echo ($M[1][0]);
于 2013-03-27T09:50:19.080 回答
1

试试下面的 php 代码,它可以在没有 pregmatch 的情况下工作

IE :

http://vimeo.com/22222222
http://player.vimeo.com/video/22222222
http://vimeo.com/channels/staffpicks/22222222
http://vimeo.com/groups/groupsname/22222222

PHP代码:

    $vimeoUrl = 'http://vimeo.com/channels/channelsName/22222222';
    $fetchVimeoIdArr = explode('/', $vimeoUrl);
    $idCounter = count($fetchVimeoIdArr) - 1;
    $vimeoId = $fetchVimeoIdArr[$idCounter];
于 2016-03-11T07:27:24.650 回答