BRAINBUZZ media
BRAINBUZZ media

Reputation: 504

Get youtube/vimeo id with pregmatch

How can I get the video ID for youtube and vimeo videos embed code? A youtube video embed code looks like this:

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

where the ID in this is dbPmWbqAJPs.

A vimeo embed code looks like this:

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

where the ID in this example is 62468031.

I don't want to replace those exact ids, but rather have a pregmatch/pregreplace that finds the id in any embed of vimeo and of youtube.

Upvotes: 1

Views: 1190

Answers (2)

Niko Jojo
Niko Jojo

Reputation: 1226

Try below code for php, it works without pregmatch

i.e. :

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

PHP Code :

    $vimeoUrl = 'http://vimeo.com/channels/channelsName/22222222';
    $fetchVimeoIdArr = explode('/', $vimeoUrl);
    $idCounter = count($fetchVimeoIdArr) - 1;
    $vimeoId = $fetchVimeoIdArr[$idCounter];

Upvotes: 1

mohammad mohsenipur
mohammad mohsenipur

Reputation: 3149

try this

vimeo

$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]);

Upvotes: 2

Related Questions