Get Facebook multiple video id options from url using RegEx in PHP

This question was asked repeatedly in Stack Overflow, and I looked around a lot, but all the answers that I found were found for only one variant of the Facebook URL, but I must be sure that you can use any of the options 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 does not work, as I expect, https://regex101.com/r/sC6oR2/6

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

I am not too familiar with RegEx, but when I searched, I tried my best. I also looked at using parse_url () , and I heard that it would be better than RegEx, but since Facebook has many different types of URLs: s.

I would really appreciate help in this, since most of the day I spent on getting it to work, when I get one part of the work, I do not get the other - and she left me with a mild headache.

+4
source share
3 answers

Not so complicated, there are only two types of formats that you described.

  • video / id
  • ? V = id

So, if he has a โ€œvideo,โ€ then just grab the last digits, but if thatโ€™s how? v, grab 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

:

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;
}
+2

:

/(\ d +) + | v = (\ d +) |. VB\d +/(\ d +)

"videos/" "v ="

https://regex101.com/r/sC6oR2/64

+2
<?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');
+1

All Articles