What are some best practices for defining the replacement string in preg_replace in PHP?

When defining the replacement string in preg_replace in PHP, it's important to use single quotes for the replacement string to prevent PHP from interpreting escape sequences within the string. This ensures that any special characters in the replacement string are treated as literals. Additionally, you can use backreferences to capture groups from the pattern match and include them in the replacement string.

$pattern = '/(\d{2})-(\d{2})-(\d{4})/';
$replacement = '$3-$1-$2';
$string = '31-12-2021';
$new_string = preg_replace($pattern, $replacement, $string);
echo $new_string; // Output: 2021-31-12