How can one efficiently store and check a large array of usernames for pattern recognition in PHP?

When dealing with a large array of usernames for pattern recognition in PHP, one efficient way to store and check them is by using regular expressions. Regular expressions allow you to define patterns that the usernames must match, making it easier to filter and identify specific usernames. You can loop through the array of usernames and use the preg_match function to check if each username matches the desired pattern.

$usernames = ['john_doe', 'jane_smith', 'admin123', 'user456'];

$pattern = '/^[a-zA-Z0-9_]{5,10}$/'; // Pattern for usernames containing alphanumeric characters and underscores, with a length between 5 and 10 characters

foreach ($usernames as $username) {
    if (preg_match($pattern, $username)) {
        echo "Username $username matches the pattern.\n";
    } else {
        echo "Username $username does not match the pattern.\n";
    }
}