What are some best practices for using preg_replace in PHP to achieve desired results?

When using preg_replace in PHP, it is important to use proper regex patterns to match the desired text and replace it accordingly. Additionally, it is recommended to use the delimiters correctly and escape any special characters that may interfere with the regex pattern. Lastly, consider using the fourth parameter of preg_replace to limit the number of replacements made.

// Example: Replace all occurrences of "apple" with "orange" in a given string
$string = "I like apple pie and apple juice.";
$pattern = "/\bapple\b/";
$replacement = "orange";
$replaced_string = preg_replace($pattern, $replacement, $string);
echo $replaced_string;