What is the purpose of using preg_match() in PHP and what are some common scenarios where it is used?

The purpose of using preg_match() in PHP is to perform a regular expression match on a string. This function is commonly used to search for a specific pattern within a string and extract or manipulate the matched content. Some common scenarios where preg_match() is used include validating user input (such as email addresses or phone numbers), extracting data from a string, and parsing text for specific patterns.

// Example usage of preg_match() to validate an email address
$email = "example@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";
}