What are some best practices for using preg_replace() in PHP to replace unwanted characters in a string?

When using preg_replace() in PHP to replace unwanted characters in a string, it is important to use proper regular expressions to target the specific characters you want to replace. Additionally, it is recommended to use the /u modifier for Unicode support and the /i modifier for case-insensitive matching if needed. Lastly, make sure to properly escape any special characters in the regular expression pattern.

$string = "This is a string with unwanted characters!@#$%";
$cleaned_string = preg_replace('/[^a-zA-Z0-9\s]/u', '', $string);
echo $cleaned_string;