How can regular expressions be used to parse and extract specific information in PHP?

Regular expressions can be used in PHP to parse and extract specific information from a string. By defining a pattern using regular expressions, you can search for and extract specific data like phone numbers, email addresses, or any other structured information. This can be useful for tasks like data validation, text processing, and extracting relevant information from a larger text.

// Example of using regular expressions to extract phone numbers from a string
$string = "My phone number is 123-456-7890. Call me!";
$pattern = '/\d{3}-\d{3}-\d{4}/'; // Regular expression pattern for matching phone numbers in the format XXX-XXX-XXXX

if (preg_match($pattern, $string, $matches)) {
    $phone_number = $matches[0];
    echo "Phone number found: " . $phone_number;
} else {
    echo "No phone number found.";
}