What are some best practices for using regular expressions in PHP functions like preg_replace?

When using regular expressions in PHP functions like preg_replace, it is important to escape any special characters that may be interpreted as regex metacharacters. This can be done using the preg_quote function to ensure that the pattern matches the literal string. Additionally, it is recommended to use delimiters that are not commonly used in the pattern to avoid conflicts. Finally, consider using the /u modifier for Unicode support if working with multibyte characters.

$pattern = '/\bword\b/';
$replacement = 'new_word';
$string = 'This is a word example';
$escaped_pattern = preg_quote($pattern, '/');
$result = preg_replace('/' . $escaped_pattern . '/', $replacement, $string);
echo $result;