Both htmlspecialchars()
and strip_tags()
are considered safe for cleaning user input for output on a HTML page.
Related:
The easiest way is to simply prevent any users from entering HTML tags. If you strip_tags() or htmlspecialchars() all user input then there is no way to introduce a <script>
tag.
If you want to allow limited markup, then you can use a bbcode-like syntax (finding a PHP library for doing this shouldn’t be hard, though I’ve never done this myself so don’t have any recommendations on that front), or you could use HTMLpurifier to restrict the markup that users are allowed to enter.