What are potential pitfalls when using regular expressions in PHP to extract specific parts of a string?
One potential pitfall when using regular expressions in PHP to extract specific parts of a string is not properly escaping special characters. This can lead to unexpected behavior or errors in the regex pattern matching. To solve this issue, you can use the `preg_quote()` function to escape special characters before using them in the regex pattern.
$string = "Hello, my email is user@example.com";
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
if (preg_match($pattern, $string, $matches)) {
echo "Email found: " . $matches[0];
}