How to escape replacement string for preg_replace?

I think the issue is if the user supplied string contains a backreference like $1 or \1 etc…