How can PHP developers effectively test and debug regular expressions used for text manipulation tasks?

Regular expressions can be complex and tricky to debug, especially when used for text manipulation tasks in PHP. One effective way for PHP developers to test and debug regular expressions is by using online tools like regex101.com or using PHP functions like preg_match() to test the regular expression against sample text. Additionally, developers can use tools like var_dump() or print_r() to inspect the output of the regular expression matches and debug any issues.

// Sample regular expression for matching email addresses
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';

// Sample text to test against
$text = 'Sample text with email address test@example.com';

// Test the regular expression against the text
if (preg_match($pattern, $text, $matches)) {
    echo 'Email address found: ' . $matches[0];
} else {
    echo 'No email address found';
}