Skip to content

Instantly share code, notes, and snippets.

@cergey-obr
Created July 11, 2014 06:28
Show Gist options
  • Save cergey-obr/b38648df5d25b2833a2e to your computer and use it in GitHub Desktop.
Save cergey-obr/b38648df5d25b2833a2e to your computer and use it in GitHub Desktop.
Получение id видео youtube $url - url видео youtube
public function getYouTubeIdFromURL($url) {
$pattern =
'%^ # Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www 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=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x';
$result = preg_match($pattern, $url, $matches);
if (false !== $result) return $matches[1];
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment