What are some common pitfalls to be aware of when using regular expressions in PHP for string manipulation?

One common pitfall when using regular expressions in PHP for string manipulation is not properly escaping special characters. This can lead to unexpected behavior or errors in the regex pattern. To solve this issue, use the preg_quote() function to escape special characters before using them in the regex pattern.

// Example of properly escaping special characters in a regex pattern
$pattern = '/^[\w\s]+$/'; // This regex pattern will match any word characters or spaces at the beginning of a string
$escaped_pattern = preg_quote($pattern, '/'); // Escape special characters in the pattern