In what scenarios is it advisable to use regular expressions over DOM manipulation in PHP?

Regular expressions are useful when you need to search for patterns within strings, such as extracting specific data from a larger text. This can be more efficient than manually parsing through the DOM structure of a webpage. Regular expressions are particularly handy when dealing with text manipulation tasks, like validating email addresses or extracting phone numbers from a block of text.

// Example of using regular expressions to extract phone numbers from a text

$text = "John's phone number is 555-1234 and Jane's is 555-5678.";
$pattern = '/\b\d{3}-\d{4}\b/';

preg_match_all($pattern, $text, $matches);

foreach ($matches[0] as $phone) {
    echo "Phone number: " . $phone . "\n";
}