Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Facebook multiple variations of Video ID from URL using RegEx in PHP

This question has been asked numerous times on Stack Overflow and I've looked around a lot, but all answers I've found only finds it for one variation of the Facebook URL, but I need to be sure that you can use any of the variations that can be used.

  • https: //www.facebook.com/username2/videos/100000000000000
  • http: //www.facebook.com/photo.php?v=107084586333124
  • http: //www.facebook.com/photo.php?more=diffictult&v=107084586333124
  • http: //www.facebook.com/?v=107084586333124

This is what I got so far, but it's doesnt quite work as I expect, https://regex101.com/r/sC6oR2/6

^https?://www.facebook.com/(\?v=\d+)|(.*?/videos/(\d+))$

I am not too familiar with RegEx but as I searched around I tried to make the best out of it. I've also looked in to using parse_url() and I've heard that it would be better than RegEx but since Facebook has many different types of URL:s.

I would highly appreciate some help with this since I've spent most of the day trying to get this to work, when I get one part to work I don't get the other one - and it've left me with a mild headache.

like image 537
Jimmy Avatar asked Mar 16 '23 11:03

Jimmy


2 Answers

<?php

/**
* Get video id from any facebook video URL
* funcion by Mohamed Elbahja
*/
 function fbvideoid($url) {
    // delete space in url
    $url =  str_replace(' ', '', $url);
    // parse url
    $pars = parse_url($url);
    $path = $pars['path'];

    // delete end slashe
    if($path[strlen($path) - 1] == '/'):
        $path = rtrim($path, '/');
    endif;

    $count = count(explode("/", $path));
    // type url
    $urltype = "";
    if ($pars['path'] == "/photo.php" || $pars['path'] == "/video.php" || $pars['path'] == "/"):
        $urltype = 2;
    elseif($count == 4):
      $urltype = 3;
    elseif($count == 5):
        $urltype = 1;
    endif;

   // get id
    if ($urltype == 1) {

        $ex = explode("/", $path);
        $videoid = $ex[4];

    } else if ($urltype == 2) {

        parse_str($pars['query'], $e);
        $videoid = $e['v'];

    } else if ($urltype == 3) {

        $ex = explode("/", $path);
        $videoid = $ex[3];

    } else {
        $videoid = null;
    }

   return $videoid;
}

    // example :) 

echo fbvideoid('https://www.facebook.com/username2/videos/107084586333124'); 

echo "<br>";

echo fbvideoid('http://www.facebook.com/photo.php?v=107084586333124');

echo "<br>";

echo fbvideoid('http://www.facebook.com/photo.php?more=diffictult&v=107084586333124');

echo "<br>";

echo fbvideoid('http://www.facebook.com/?v=107084586333124');

echo "<br>";

echo fbvideoid('http://www.facebook.com/video.php?v=107084586333124');

echo "<br>";

echo fbvideoid('https://www.facebook.com/LovehelloU/videos/vb.172573132808153/107084586333124/?type=2&theater');
like image 158
Mohamed Elbahja Avatar answered Mar 18 '23 07:03

Mohamed Elbahja


Not really that difficult, there are only two types of formats that you have described.

  • videos/id
  • ?v=id

So if it has 'videos', then just grab the last digits, and if it as ?v, grab the v from the url.

<?php

$urls = ['https://www.facebook.com/username2/videos/100000000000000',
         'http://www.facebook.com/photo.php?v=107084586333124',
         'http://www.facebook.com/photo.php?more=diffictult&v=107084586333124',
         'http://www.facebook.com/?v=107084586333124'];

$ids = [];

foreach ($urls as $url) {
    $tmp = explode('/', $url);
    if (strtolower($tmp[count($tmp) - 2] == 'videos')) {
        $ids[$url] = $tmp[count($tmp) - 1];
        continue;
    }
    parse_str(parse_url($url)['query'], $query);
    if (!empty($query['v']))
        $ids[$url] = $query['v'];
}

print_r($ids);

Output:

Array
(
    [https://www.facebook.com/username2/videos/100000000000000] => 100000000000000
    [http://www.facebook.com/photo.php?v=107084586333124] => 107084586333124
    [http://www.facebook.com/photo.php?more=diffictult&v=107084586333124] => 107084586333124
    [http://www.facebook.com/?v=107084586333124] => 107084586333124

Here's a function just for this:

function getID($url) {
    $tmp = explode('/', $url);
    if (strtolower($tmp[count($tmp) - 2] == 'videos'))
        return $tmp[count($tmp) - 1];
    parse_str(parse_url($url)['query'], $query);
    if (!empty($query['v']))
        return $query['v'];
    return false;
}
like image 31
Dave Chen Avatar answered Mar 18 '23 07:03

Dave Chen