What are common pitfalls when using preg_replace to remove special characters in PHP?

When using preg_replace to remove special characters in PHP, a common pitfall is not properly escaping special characters in the regular expression pattern. This can lead to unexpected behavior or errors in the replacement process. To avoid this issue, it is important to escape special characters using preg_quote() before constructing the regular expression pattern.

// Example code snippet to remove special characters using preg_replace with proper escaping
$string = "Hello, @world!";
$pattern = '/[@#\$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/';
$cleaned_string = preg_replace($pattern, '', $string);
echo $cleaned_string; // Output: Hello world