Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the PHP function <a href="http://php.net/manual/en/function.parse-url.php" rel="noreferrer"><code>parse_url</code></a> to extract host name, path, query string and the fragment. You can then use PHP string functions to locate the video id.</p> <pre><code>function getYouTubeVideoId($url) { $video_id = false; $url = parse_url($url); if (strcasecmp($url['host'], 'youtu.be') === 0) { #### (dontcare)://youtu.be/&lt;video id&gt; $video_id = substr($url['path'], 1); } elseif (strcasecmp($url['host'], 'www.youtube.com') === 0) { if (isset($url['query'])) { parse_str($url['query'], $url['query']); if (isset($url['query']['v'])) { #### (dontcare)://www.youtube.com/(dontcare)?v=&lt;video id&gt; $video_id = $url['query']['v']; } } if ($video_id == false) { $url['path'] = explode('/', substr($url['path'], 1)); if (in_array($url['path'][0], array('e', 'embed', 'v'))) { #### (dontcare)://www.youtube.com/(whitelist)/&lt;video id&gt; $video_id = $url['path'][1]; } } } return $video_id; } $urls = array( 'http://youtu.be/dQw4w9WgXcQ', 'http://www.youtube.com/?v=dQw4w9WgXcQ', 'http://www.youtube.com/?v=dQw4w9WgXcQ&amp;feature=player_embedded', 'http://www.youtube.com/watch?v=dQw4w9WgXcQ', 'http://www.youtube.com/watch?v=dQw4w9WgXcQ&amp;feature=player_embedded', 'http://www.youtube.com/v/dQw4w9WgXcQ', 'http://www.youtube.com/e/dQw4w9WgXcQ', 'http://www.youtube.com/embed/dQw4w9WgXcQ' ); foreach ($urls as $url) { echo sprintf('%s -&gt; %s' . PHP_EOL, $url, getYouTubeVideoId($url)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload