0

I am trying to retrieve a video ID from a link, but the returned result is NULL:

$myurl = "https://www.youtube.com/watch?v=-aSJ2nRUcTk&list=PLIaLMmGmfJ03L5A1Xoyxa_034xxSurqrO";

preg_match('/?v=(.*)&(index|list)/', $myurl, $match);

var_dump($match);

I am looking for this video ID: -aSJ2nRUcTk

Thanks in advance

Oum Alaa
  • 227
  • 3
  • 11

1 Answers1

4

You could do it without regex via exploding on the ? (or doing a substring or other method to get only what's after the ?) and parsing the query string into an array with parse_str

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

$str = end(explode("?","https://www.youtube.com/watch?v=-aSJ2nRUcTk&list=PLIaLMmGmfJ03L5A1Xoyxa_034xxSurqrO"));
$params = parse_str($str);
echo $params['v'];
// will echo -aSJ2nRUcTk
skrilled
  • 5,350
  • 2
  • 26
  • 48