What potential issues can arise when using preg_replace with regex in PHP?
One potential issue when using preg_replace with regex in PHP is that the regex pattern may not be properly escaped, leading to unexpected results or errors. To solve this, it is important to use the preg_quote function to escape any special characters in the regex pattern before using it in preg_replace.
$pattern = '/[a-z]+/';
$replacement = 'word';
$string = 'Replace all lowercase words';
$escaped_pattern = preg_quote($pattern, '/');
$result = preg_replace('/' . $escaped_pattern . '/', $replacement, $string);
echo $result;