Created
July 22, 2017 18:26
-
-
Save ConnerAiken/929cb017bca2ad974bd77ea9a7c71964 to your computer and use it in GitHub Desktop.
Clean email form input in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cleanInput($input) { | |
// Pass the $_GET, $_POST or $_REQUEST array | |
$output = array(); | |
foreach ($input as $key=>$value) { | |
$o = $value; | |
// Make sure it's within the max length | |
$o = substr($o,0,256); | |
// Tidy up line breaks | |
$o = preg_replace('/\n{2,}/', "\n\n", $o); | |
$o = nl2br($o,FALSE); | |
// Strip any odd characters | |
$o = preg_replace('/[^A-Za-z0-9\. -\!\?\(\)\<\>\@]/', "", $o); | |
// Put the data back in the array | |
$output[$key] = $o; | |
} | |
// Return the array | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment