PHP has addslashes, Perl has quotemeta.
Remember that! It’ll automagically escape all special characters from a string, which is useful if you didn’t create that string :)
PS: I’m only writing this down as no one on the rest of the internet seems to have done so very clearly.