DEV Community

Matt Kenefick
Matt Kenefick

Posted on

Regex: Extract YouTube ID from URL

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})%i', $url, $match)) {
    return $match[1];
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)