What are common validation tasks performed using preg_match in PHP?
When using preg_match in PHP for validation tasks, common tasks include checking if a string matches a specific pattern, such as validating email addresses, phone numbers, or URLs. This function allows you to perform pattern matching using regular expressions to ensure the input data meets certain criteria.
// Validate an email address using preg_match
$email = "example@example.com";
if (preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/", $email)) {
echo "Email address is valid.";
} else {
echo "Invalid email address.";
}