Created
May 9, 2011 16:08
-
-
Save felds/962794 to your computer and use it in GitHub Desktop.
Escapes scalar values deeply
This file contains 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
<?php | |
/** | |
* Escapes scalar values deeply | |
* | |
* Examples: | |
* <code> | |
* var_export(mysql_real_escape_string_deep(1)); | |
* // outputs '1' | |
* | |
* var_export(mysql_real_escape_string_deep("please escape ' me")); | |
* // outputs 'please escape \\\' me' | |
* | |
* var_export(mysql_real_escape_string_deep(array(1, "please escape ' me"))); | |
* // outputs: array (0 => '1', 1 => 'please escape \\\' me') | |
* <code> | |
* | |
* @author Luiz "Felds" Liscia <dev@felds.com.br> | |
* @params mixed $var The variable to be escaped | |
* @return mixed | |
*/ | |
function mysql_real_escape_string_deep($var) | |
{ | |
if (is_scalar($var)) | |
$var = mysql_real_escape_string($var); | |
if (is_array($var)) | |
foreach ($var as &$v) | |
$v = mysql_real_escape_string_deep($v); | |
return $var; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment