function serialize_js($data){
switch(gettype($data)){
case 'NULL': return 'null';
case 'unknown type': return 'undefined';
case 'boolean' : return $data ? 'true' : 'false';
case 'double': case 'integer': return $data;
case 'resource': case 'string': return '"' . addslashes($data). '"';
case 'array':
$isO = false;
foreach($data as $k=>$v)
if(!is_numeric($k))
$isO = true;
if(!$isO){
$rtn = array();
for($x = 0, $length = count($data); $x < $length; $x++){
$rtn[] = serialize_js($data[$x]);
}
return '['.implode(',', $rtn).']';
}
// no break;
case 'object':
$rtn = array();
foreach($data as $k=>$v){
if(is_string($k)){
$k = '"' . addslashes($k). '"';
}
$rtn[] = $k .':'.serialize_js($v);
}
return '{'.implode(',', $rtn).'}';
case 'user function':
return 'function(){alert("Error : PHP Function Call");}';
}
return '';
}
$a->a = "asd";
$a->a123 = "asd";
$data = array(
"a'sd",
3 => "z'xc",
"zxc" => 324,
"asd" => true
);
//echo serialize_js($data);
//echo serialize_js(array(1,2,3,4,$data));
?>
asdsadasdasdasdasdasdasdasdasdadasdasdasdasdasd