user317005
user317005

Reputation:

How do I get the Video Id from the URL? (DailyMotion)

Example:

http://www.dailymotion.com/video/x4xvnz_the-funny-crash-compilation_fun

How do I get x4xvnz?

Upvotes: 6

Views: 16771

Answers (5)

ling
ling

Reputation: 10017

I use this:

function getDailyMotionId($url)
{

    if (preg_match('!^.+dailymotion\.com/(video|hub)/([^_]+)[^#]*(#video=([^_&]+))?|(dai\.ly/([^_]+))!', $url, $m)) {
        if (isset($m[6])) {
            return $m[6];
        }
        if (isset($m[4])) {
            return $m[4];
        }
        return $m[2];
    }
    return false;
}

It can handle various urls:

$dailymotion = [
    'http://www.dailymotion.com/video/x2jvvep_coup-incroyable-pendant-un-match-de-ping-pong_tv',
    'http://www.dailymotion.com/video/x2jvvep_rates-of-exchange-like-a-renegade_music',
    'http://www.dailymotion.com/video/x2jvvep',
    'http://www.dailymotion.com/hub/x2jvvep_Galatasaray',
    'http://www.dailymotion.com/hub/x2jvvep_Galatasaray#video=x2jvvep',
    'http://www.dailymotion.com/video/x2jvvep_hakan-yukur-klip_sport',
    'http://dai.ly/x2jvvep',
];

Check out my github (https://github.com/lingtalfi/video-ids-and-thumbnails/blob/master/testvideo.php), I provide functions to get ids (and also thumbnails) from youtube, vimeo and dailymotion.

Upvotes: 2

chetanspeed511987
chetanspeed511987

Reputation: 2025

preg_match('#<object[^>]+>.+?http://www.dailymotion.com/swf/video/([A-Za-z0-9]+).+?</object>#s', $dailymotionurl, $matches);

        // Dailymotion url
        if(!isset($matches[1])) {
            preg_match('#http://www.dailymotion.com/video/([A-Za-z0-9]+)#s', $dailymotionurl, $matches);
        }

        // Dailymotion iframe
        if(!isset($matches[1])) {
            preg_match('#http://www.dailymotion.com/embed/video/([A-Za-z0-9]+)#s', $dailymotionurl, $matches);
        }
 $id =  $matches[1];

Upvotes: 2

Eddie
Eddie

Reputation: 13116

<?php
$output = parse_url("http://www.dailymotion.com/video/x4xvnz_the-funny-crash-compilation_fun");

// The part you want
$url= $output['path'];
$parts = explode('/',$url);
$parts = explode('_',$parts[2]);

echo $parts[0];

http://php.net/manual/en/function.parse-url.php

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816324

You can use basename [docs] to get the last part of the URL and then strtok [docs] to get the ID (all characters up to the first _):

$id = strtok(basename($url), '_');

Upvotes: 7

Mat
Mat

Reputation: 206679

/video\/([^_]+)/

should do the trick. This grabs in the first capture all text after video/ up till the first _.

Upvotes: 4

Related Questions