Earlier today a question was asked regarding input validation strategies in web apps.
The top answer, at time of writing, suggests in PHP
just using
In addition to Cheekysoft's excellent answer:
There isn't really a silver bullet for preventing HTML injection (e.g. cross site scripting), but you may be able to achieve it more easily if you're using a library or templating system for outputting HTML. Read the documentation for that for how to escape things appropriately.
In HTML, things need to be escaped differently depending on context. This is especially true of strings being placed into Javascript.