Oum Alaa
Oum Alaa

Reputation: 227

Retrieve the video ID

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

Upvotes: 0

Views: 38

Answers (1)

skrilled
skrilled

Reputation: 5371

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

Upvotes: 4

Related Questions