Given a YouTube video URL, parse out the video ID.
Install the package via composer:
composer require waynestate/parse-youtube-id
Parse full YouTube URL's
use Waynestate\Youtube\ParseId;
...
$youtube_id = ParseId::fromUrl('https://www.youtube.com/watch?v=yCjTG0rOIXQ');
echo $youtube_id; // 'yCjTG0rOIXQ'
Parse short YouTube URL's
$youtube_id = ParseId::fromUrl('https://youtu.be/yCjTG0rOIXQ');
echo $youtube_id; // 'yCjTG0rOIXQ'
Null strings or non-YouTube URL's return an empty string
$youtube_id = ParseId::fromUrl('');
echo $youtube_id; // ''
See /tests/UrlParseTest.php
for all supported URL variations.
$pattern = '#^(?:https?://|//)?' # Optional URL scheme. Either http, or https, or protocol-relative.
. '(?:www\.|m\.)?' # Optional www or m subdomain.
. '(?:' # Group host alternatives:
. 'youtu\.be/' # Either youtu.be,
. '|youtube\.com/' # or youtube.com
. '(?:' # Group path alternatives:
. 'embed/' # Either /embed/,
. '|v/' # or /v/,
. '|watch\?v=' # or /watch?v=,
. '|watch\?.+&v=' # or /watch?other_param&v=
. ')' # End path alternatives.
. ')' # End host alternatives.
. '([\w-]{11})' # 11 characters (Length of Youtube video ids).
. '(?![\w-])#'; # Rejects if overlong id.
composer test
See CONTRIBUTING.md for details
Adapted from Stephan Schmitz at https://3v4l.org/GEDT0
Wayne State University (WSU) is an urban public research university located in Detroit, Michigan.