Posted By

cshaiku on 06/30/09


Tagged

regex input sanitize


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

cshaiku
vali29
chrisjlee


Sanitize Input


 / Published in: PHP
 

URL: http://code.cshaiku.com/code_php_sanitize_input.php

  1. function _codeSanitize($input, $methods) {
  2.  
  3. if (strlen($methods) > 0) {
  4. if (in_array('alpha', $methods))
  5. $input = ereg_replace("[A-Za-z]", "", $input);
  6.  
  7. if (in_array('numeric', $methods))
  8. $input = ereg_replace("[0-9]", "", $input);
  9.  
  10. if (in_array('nonalphanumeric', $methods))
  11. return ereg_replace("[^A-Za-z0-9]", "", $input);
  12. }
  13. return $input;
  14. }
  15.  
  16. ###
  17. $dirty = '';
  18. $sanitized = '';
  19.  
  20. if (@strlen($_POST['dirty']) > 0) {
  21. $dirty = stripslashes(htmlspecialchars($_POST['dirty']));
  22. @$sanitized = _codeSanitize($dirty, $_POST['methods']);
  23. }

Report this snippet  

You need to login to post a comment.