What are common pitfalls when using regex for parsing YouTube links in PHP?

One common pitfall when using regex for parsing YouTube links in PHP is not accounting for the various formats in which the links can appear, such as with or without the "https://" prefix, or with additional parameters. To solve this, it is important to create a regex pattern that can handle these variations.

// Example of a regex pattern to parse YouTube links in PHP
$pattern = '/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\w{11})|youtu\.be\/(\w{11}))/';
$url = "https://www.youtube.com/watch?v=abcdefghijk";

if (preg_match($pattern, $url, $matches)) {
    $video_id = end($matches);
    echo "YouTube video ID: " . $video_id;
} else {
    echo "Invalid YouTube link";
}