What are some common pitfalls when using regular expressions in PHP, especially when trying to exclude special characters?

When trying to exclude special characters using regular expressions in PHP, a common pitfall is not escaping the special characters properly. To exclude special characters, you can use the \w pattern to match word characters (letters, numbers, and underscores) and then negate it by using a caret (^) inside a character class. This will match any character that is not a word character.

$string = "Hello, World! 123";
$clean_string = preg_replace('/[^\w\s]/', '', $string);
echo $clean_string; // Output: Hello World 123