How can PHP be used to restrict input to only alphanumeric characters?

To restrict input to only alphanumeric characters in PHP, you can use a regular expression to validate the input. By checking if the input contains only letters and numbers, you can ensure that no special characters are allowed. This can be done using the preg_match function in PHP.

$input = "abc123"; // Input to be validated

if (preg_match('/^[a-zA-Z0-9]+$/', $input)) {
    echo "Input is alphanumeric";
} else {
    echo "Input contains special characters";
}