What is the function of preg_match in PHP and how is it commonly used?

The preg_match function in PHP is used to perform a regular expression match on a string. It returns true if a match is found, and false otherwise. This function is commonly used for validating input, extracting specific data from a string, or checking if a string conforms to a certain pattern.

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

if(preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/", $email)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}