在插入数据库时,我们会使用addslashes对",',\等进行过滤
下边介绍一个函数,采用递归,数组或者字符串类型的变量进行过滤
function daddslashes($string){
if(is_array($string)) {
foreach($string as $key=>$val){
$str[addslashes($key)] = daddslashes($val);
}
} else {
$str = addslashes($string);
}
return $str;
}
eg:
$array = array("i'color" => array("bl'ue", "red", "green"),
"size" => array("sma\ll", "medium", "large"));
$a = daddslashes($array);
结果为:
array 'i\'color' => array 0 => string 'bl\'ue' (length=6) 1 => string 'red' (length=3) 2 => string 'green' (length=5) 'size' => array 0 => string 'sma\\ll' (length=7) 1 => string 'medium' (length=6) 2 => string 'large' (length=5)