Skip to content

Instantly share code, notes, and snippets.

@ConnerAiken
Created July 22, 2017 18:26
Show Gist options
  • Save ConnerAiken/929cb017bca2ad974bd77ea9a7c71964 to your computer and use it in GitHub Desktop.
Save ConnerAiken/929cb017bca2ad974bd77ea9a7c71964 to your computer and use it in GitHub Desktop.
Clean email form input in PHP
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