What are common pitfalls when using regular expressions in PHP for extracting specific information from strings?

A common pitfall when using regular expressions in PHP for extracting specific information from strings is not properly escaping special characters. This can lead to unexpected results or errors in the regex pattern matching. To solve this, make sure to use the preg_quote() function to escape any special characters in the input string before using it in the regex pattern.

$input_string = "Hello, this is a test string with special characters like ^ and $";
$escaped_string = preg_quote($input_string, '/');
$pattern = '/special characters like \^ and \$/';
if(preg_match($pattern, $escaped_string, $matches)){
    echo "Match found: " . $matches[0];
} else {
    echo "No match found.";
}