How can regular expressions be used to check for specific character patterns in a string in PHP?

Regular expressions can be used in PHP to check for specific character patterns in a string by using functions like preg_match(). This function allows you to define a pattern using regular expression syntax and then check if the string matches that pattern. For example, if you want to check if a string contains only letters and numbers, you can use the pattern '/^[a-zA-Z0-9]*$/'.

$string = "Hello123";
$pattern = '/^[a-zA-Z0-9]*$/';

if (preg_match($pattern, $string)) {
    echo "String contains only letters and numbers.";
} else {
    echo "String contains other characters.";
}