What are best practices for organizing and maintaining multiple regex patterns for different types of video links in PHP?

When dealing with multiple regex patterns for different types of video links in PHP, it is best to organize them in an array for easier management and maintenance. By storing each regex pattern in an associative array with a corresponding key for the video type, you can easily access and apply the correct pattern based on the video link type. This approach helps keep your code clean, organized, and scalable.

// Define an associative array to store regex patterns for different video link types
$videoPatterns = [
    'youtube' => '/^(https?:\/\/)?(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})/',
    'vimeo' => '/^(https?:\/\/)?(www\.)?(vimeo\.com\/)([0-9]{8})/',
    // Add more video link types and corresponding regex patterns as needed
];

// Function to match a video link with the appropriate regex pattern
function matchVideoLink($url, $videoPatterns) {
    foreach ($videoPatterns as $type => $pattern) {
        if (preg_match($pattern, $url, $matches)) {
            return $type;
        }
    }
    return false;
}

// Example usage
$url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
$videoType = matchVideoLink($url, $videoPatterns);
if ($videoType) {
    echo "The video link is of type: $videoType";
} else {
    echo "Could not match the video link type";
}