Skip to content

Instantly share code, notes, and snippets.

@iamkirkbater
Last active January 2, 2016 18:09
Show Gist options
  • Save iamkirkbater/f5bcd30e14adbc63213a to your computer and use it in GitHub Desktop.
Save iamkirkbater/f5bcd30e14adbc63213a to your computer and use it in GitHub Desktop.
Sanitize a form for input into SQL db. Strips out any Word special characters, too.
<?php
function sanitize(&$item) {
$item = str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$item);
$item = str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$item);
$item = trim($item);
$item = stripslashes($item);
$item = htmlentities($item, ENT_QUOTES);
$item = strip_tags($item);
return $item;
}
$sub = $_POST['submitted'];
//Recursively sanitize the submitted data:
array_walk_recursive($sub,'sanitize');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment