There are already native functions to escape for HTML and JS strings: htmlspecialchars()
and json_encode()
. See this related question on Stack Overflowthis related question on Stack Overflow
As for innerHTML
, simply don't use it. Use textContent
instead. If you wish to allow for formatting (for example, in comments or posts), I recommend Markdown