In what situations is it appropriate to use regular expressions for string manipulation in PHP?

Regular expressions are appropriate for string manipulation in PHP when you need to search for or replace specific patterns within a string. This can be useful for tasks such as validating input, extracting data, or formatting text. Regular expressions provide a powerful and flexible way to work with strings that match certain criteria.

// Example: Using regular expressions to extract email addresses from a string
$string = "Contact us at email@example.com or support@example.com for assistance.";
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
preg_match_all($pattern, $string, $matches);
$emails = $matches[0];

foreach ($emails as $email) {
    echo $email . "\n";
}