String to database (sanitize string for inserting in database)


/ Published in: PHP
Save to your folder(s)

This function can be used to sanitize single string variable or massive like POST, GET, COOKIE. It performs magic quotes gpc check, strip tags, trim and escape the dangerous signs with mysql_real_escape_string.


Copy this code and paste it in your HTML
  1. function str2db($input, $strip_tags=true) {
  2. if(is_array($input)) {
  3. foreach($input as $key => $value) {
  4. $input[$key] = str2db($value);
  5. }
  6. } else {
  7. if(ini_get('magic_quotes_sybase')){
  8. $input = str_replace("''", "'", $input);
  9. }
  10. else {
  11. $input = stripslashes($input);
  12. }
  13. }
  14. if($strip_tags) {
  15. $input = strip_tags($input);
  16. }
  17. $input = mysql_real_escape_string($input);
  18. $input = trim($input);
  19. }
  20. return $input;
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.