6

Example:

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

How do I get x4xvnz?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143

5 Answers5

7

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), '_');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
4
/video\/([^_]+)/

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

Mat
  • 202,337
  • 40
  • 393
  • 406
2

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.

ling
  • 9,545
  • 4
  • 52
  • 49
  • this function is very nice. Let me however add a slight update : it's failing on this case : `https://www.dailymotion.com/video/x7bm45u?playlist=x6ffqw` to handle it, just change `([^_]+)` by `([^_?]+)` – Fenix Aoras Jul 17 '19 at 22:09
2
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];
chetanspeed511987
  • 1,995
  • 2
  • 22
  • 34
0
<?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

Eddie
  • 12,898
  • 3
  • 25
  • 32