To extract the identifier from the following URL:
$url = 'www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2';
You can use parse_url() first to get the query string:
$queryString = parse_url($url, PHP_URL_QUERY); var_dump($queryString);
What, here, will give you:
string 'v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2' (length=49)
And then use parse_str() to extract the parameters from this query string:
parse_str($queryString, $params); var_dump($params);
Which will give you the following array:
array 'v' => string 'B4CRkpBGQzU' (length=11) 'feature' => string 'youtube_gdata' (length=13) 'par1' => string '1' (length=1) 'par2' => string '2' (length=1)
And now, it's just a matter of using the v element from this array by typing it into the sketch url:
if (isset($params['v'])) { echo "i3.ytimg.com/vi/{$params['v']}/default.jpg"; }
What gives:
i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg
source share