What is the correct syntax to exclude files starting with a specific string when using glob()?

When using the glob() function in PHP to retrieve files, there is no built-in option to exclude files starting with a specific string. However, you can achieve this by using the array_filter() function in combination with glob(). By applying a custom filter function, you can exclude files based on their names. This allows you to retrieve files matching a certain pattern while excluding those that start with a specific string.

// Define the directory path and pattern
$directory = 'path/to/directory/';
$pattern = '*.txt';

// Get all files matching the pattern and exclude files starting with 'exclude'
$files = array_filter(glob($directory . $pattern), function($file) {
    return !preg_match('/^exclude/', basename($file));
});

// Output the list of files
print_r($files);