What is the function preg_match() in PHP and how can it be used for data validation?

The preg_match() function in PHP is used for pattern matching with regular expressions. It can be used for data validation by checking if a string matches a specific pattern or format. This can be useful for validating input such as email addresses, phone numbers, or passwords to ensure they meet certain criteria.

// Example of using preg_match() for data validation
$email = "example@example.com";

if (preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/", $email)) {
    echo "Email is valid.";
} else {
    echo "Email is invalid.";
}