What are common issues when using preg_replace to replace strings in PHP?

One common issue when using preg_replace in PHP is that special characters in the replacement string can interfere with the regular expression pattern. To solve this issue, you can use the preg_quote function to escape any special characters in the replacement string before passing it to preg_replace.

$pattern = '/\bapple\b/';
$replacement = 'orange$1'; // $1 is a special character that can interfere
$replacement = preg_quote($replacement, '/');
$result = preg_replace($pattern, $replacement, $input);