How can special characters like periods be escaped or masked in preg_match in PHP?

Special characters like periods need to be escaped in regular expressions used with preg_match in PHP to ensure they are treated as literal characters. This can be done by using a backslash (\) before the special character to escape it. For example, to match a period in a string, you would need to escape it as \..

$string = "Hello. World";
$pattern = "/\./";
if (preg_match($pattern, $string)) {
    echo "Period found in the string.";
} else {
    echo "Period not found in the string.";
}