What are some common pitfalls when using regular expressions in PHP, specifically with preg_match?

One common pitfall when using regular expressions in PHP with preg_match is not properly escaping special characters. To avoid this issue, you should use the preg_quote function to escape any characters that have special meaning in regular expressions.

// Incorrect way without escaping special characters
$pattern = '/[a-z]+/';
$string = 'hello world';

if (preg_match($pattern, $string)) {
    echo 'Match found';
} else {
    echo 'No match found';
}

// Correct way with escaping special characters
$pattern = '/'. preg_quote('[a-z]+') .'/';
$string = 'hello world';

if (preg_match($pattern, $string)) {
    echo 'Match found';
} else {
    echo 'No match found';
}