What are some common pitfalls when using regex to manipulate strings in PHP?

One common pitfall when using regex to manipulate strings in PHP is not escaping special characters properly. To avoid this issue, make sure to use the preg_quote() function to escape any special characters in the regex pattern. This will ensure that the pattern matches the desired string correctly.

$string = "Hello, [world]!";
$pattern = "/\[[world]\]/";
$escaped_pattern = preg_quote($pattern, '/');
if (preg_match($escaped_pattern, $string)) {
    echo "Pattern found in string.";
} else {
    echo "Pattern not found in string.";
}