How can I validate a HTML code submitted by the user? Looking for <script>
tags is easy, but you can also embed JS in for example <div onclick="yyy"></div>
. Are there any ready to use libraries/functions? (like $safeHTML = validateCode($rawHTML)
)
Asked
Active
Viewed 93 times
0

user1019812
- 5
- 2
-
1Do you want to *validate* or *sanitize*? – JJJ Oct 29 '11 at 13:46
2 Answers
-1
I use the following function:
function sanitizeString($var){
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
return mysql_real_escape_string($var);
It changes over characters like < to
<
prevent escape characters for SQL, stips unwanted slashes, etc.

Dennis
- 138
- 1
- 4
-
1-1 This doesn't validate anything and looks like a bad collection of different stuff. `strip_tags()` might break your html, makes it unusable later and doesn't like malformed tags (something like `<
script>`, or similar); `stripslashes()` might be useful only in the ever more rare event of magic_quotes enabled; `mysql_real_escape_string()` is a MySQL escaping function, and doesn't work without an open connection (returning FALSE from all your beautiful sanitizer) or with INTs; HTML and DB are irrelated destination,need to be treated as such. Escape only for the target, not _a priori_ – Damien Pirsy Oct 29 '11 at 14:37