How can regular expressions be utilized in PHP to extract specific information from a string?

Regular expressions can be utilized in PHP to extract specific information from a string by using functions like preg_match() or preg_match_all(). These functions allow you to define a pattern that matches the information you want to extract and then extract that information from the input string. By using regular expressions, you can search for specific patterns, such as email addresses, phone numbers, or other structured data, within a larger string.

$input_string = "Hello, my email is test@example.com and my phone number is 555-123-4567.";
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/'; // Regular expression pattern for email address

if (preg_match($pattern, $input_string, $matches)) {
    echo "Email found: " . $matches[0];
} else {
    echo "Email not found.";
}