How can regular expressions (Regex) be utilized in PHP to efficiently parse and filter data from a text block?

Regular expressions (Regex) can be utilized in PHP to efficiently parse and filter data from a text block by defining patterns to match specific strings or characters within the text. This allows for targeted extraction or manipulation of data based on the defined criteria, making it a powerful tool for text processing tasks.

// Example of using Regex to parse and filter data from a text block
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phone number: 123-456-7890. Email: example@email.com";
$phone_pattern = '/\d{3}-\d{3}-\d{4}/'; // Regex pattern to match phone number format
$email_pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/'; // Regex pattern to match email format

// Extract phone number using Regex
preg_match($phone_pattern, $text, $phone_matches);
$phone_number = $phone_matches[0] ?? '';

// Extract email using Regex
preg_match($email_pattern, $text, $email_matches);
$email = $email_matches[0] ?? '';

// Output extracted data
echo "Phone number: $phone_number\n";
echo "Email: $email\n";