In PHP, how can regex patterns be modified to ignore empty fields or spaces while still requiring a certain minimum number of digits in the input?

When using regex patterns in PHP to validate input and require a minimum number of digits, you can modify the pattern to ignore empty fields or spaces by using the "\s*" to match zero or more whitespace characters. This allows you to enforce the minimum number of digits while still allowing for optional spaces or empty fields.

$input = " 12345"; // Example input with leading spaces
$pattern = "/^\s*\d{5,}\s*$/"; // Regex pattern to match at least 5 digits with optional leading/trailing spaces

if (preg_match($pattern, $input)) {
    echo "Input is valid.";
} else {
    echo "Input is invalid.";
}