I have a variable which could possibly, but not necessarily, contain a ' character (single quote). I have to replace any single quotes in this variable with \' (so as to escape the single quote when I use the variable in an html tag). How can I do this? I've looked into preg_replace, but it's all a bit complicated, and I can't seem to quite get it right. Thanks in advance,