How can the pattern attribute in HTML be used to enforce input restrictions in PHP forms?

The pattern attribute in HTML can be used to enforce input restrictions in PHP forms by specifying a regular expression that the input value must match. This can help validate user input and prevent incorrect data from being submitted. In PHP, you can access the submitted form data using the $_POST superglobal and then validate it using the preg_match function with the specified regular expression.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $input = $_POST['input_field'];
    
    // Define the pattern using a regular expression
    $pattern = '/^[A-Za-z]+$/';
    
    // Validate the input using preg_match
    if (!preg_match($pattern, $input)) {
        echo "Invalid input. Please enter only letters.";
    } else {
        // Input is valid, proceed with processing the form data
    }
}
?>