What is the function of preg_match in PHP and how is it used?
The preg_match function in PHP is used to perform a regular expression match on a string. It searches a given string for a match to a specified pattern and returns true if a match is found, otherwise it returns false. This function is commonly used for pattern matching and validation in PHP.
// Example usage of preg_match to validate an email address
$email = "example@example.com";
$pattern = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/";
if (preg_match($pattern, $email)) {
echo "Valid email address";
} else {
echo "Invalid email address";
}