Are there any built-in PHP functions that can automatically detect and replace spaces in file names?

When working with file names in PHP, it is common to encounter spaces that can cause issues when trying to manipulate or access files. To automatically detect and replace spaces in file names, you can use the `str_replace()` function in combination with the `glob()` function to iterate through files in a directory and rename them accordingly.

$directory = 'path/to/directory/';

foreach (glob($directory . '*') as $file) {
    $newFileName = str_replace(' ', '_', $file);
    if ($file != $newFileName) {
        rename($file, $newFileName);
    }
}