In what scenarios would it be more appropriate to use preg_replace instead of ereg_replace in PHP code?
preg_replace should be used instead of ereg_replace in PHP code when working with regular expressions, as preg_replace offers more flexibility and functionality. ereg_replace is a deprecated function and should be avoided in newer PHP versions. preg_replace allows for the use of more advanced regular expressions and offers better performance.
// Using preg_replace instead of ereg_replace
$pattern = '/\b(\w+)\b/';
$replacement = '<b>$1</b>';
$string = 'This is a sample string.';
$new_string = preg_replace($pattern, $replacement, $string);
echo $new_string;