What is the purpose of using preg_match in PHP and what are common applications for it?

The purpose of using preg_match in PHP is to search a string for a specific pattern, specified by a regular expression, and return true if the pattern is found or false if it is not found. Common applications for preg_match include validating user input (such as email addresses or phone numbers), extracting data from strings, and parsing text.

// Example of using preg_match to validate an email address
$email = "john.doe@example.com";

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