Created
June 22, 2012 07:00
-
-
Save mattpass/2970878 to your computer and use it in GitHub Desktop.
PHP var security
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
Looking for functions to clean vars for different situations. | |
Can you improve on these, returning strings, urls and numbers: | |
function strClean($var) { | |
// returns converted entities where there are HTML entity equivalents | |
return htmlentities($var, ENT_QUOTES, "UTF-8"); | |
} | |
function urlClean($var) { | |
// returns a-z A-Z 0-9 / - . _ chars only | |
return preg_replace('/[^a-zA-Z0-9\/\-\._]/si','',$var); | |
} | |
function numClean($var) { | |
// returns a number, whole or decimal or null | |
return is_numeric($var) ? floatval($var) : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To clean an URL I would use
parse_url()
and work with the returned array. To clean a number I always use(int)$var;
and(float)$var;
. You can useintval($var);
andfloatval($var);
if you want. Also there is anis_numeric()
function so your code may look like this: