36 ####################### THIS NEEDS TO BE FINISHED - JS NEEDS TO BE UPDATED
38 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'] = chr(27);
39 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'] = Array(
"\r",
"\n",
'/',
'<',
'>');
40 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_TO_LIST'] = Array(
41 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'].
'r',
42 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'].
'n',
43 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'].
'/',
44 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'].
'<',
45 $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'].
'>',
58 function var_serialise($var, $string_escape=
false)
60 $str = _var_serialise($var,
'');
61 if ($str !==
false && $string_escape) {
62 $str = str_replace(
"\n",
"\\n", addslashes($str));
77 function _var_serialise(&$var, $indent)
81 $type = strtolower(gettype($var));
86 if (strpos($var, $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR']) !== FALSE) {
87 trigger_error(
'Your data contained the escape character (ASCII Code '.ord($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR']).
'), this has been removed', E_USER_NOTICE);
88 $var = str_replace($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'],
'', $var);
90 $var = str_replace($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $var);
94 $str .=
'<val_type>'.$type.
'</val_type><val>'.$var.
"</val>\n";
98 $str .=
'<val_type>'.$type.
"</val_type><val></val>\n";
102 $str .=
'<val_type>'.$type.
'</val_type><val>'.(($var) ? 1 : 0).
"</val>\n";
107 $str .=
'<val_type>'.$type.
"</val_type>\n";
108 for (reset($var); NULL !== ($k = key($var)); next($var)) {
109 $sub_str = _var_serialise($var[$k], $indent.
' ');
110 if ($sub_str ===
false)
return false;
112 if (strpos($k, $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR']) !== FALSE) {
113 trigger_error(
'Your data contained the escape character (ASCII Code '.ord($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR']).
'), this has been removed', E_USER_NOTICE);
114 $k = str_replace($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_CHAR'],
'', $k);
116 $k = str_replace($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $k);
118 $str .= $indent.
' <name_type>'.strtolower(gettype($k)).
'</name_type><name>'.$k.
'</name>'.$sub_str;
124 trigger_error(
'Unable to serialise a var of type "'.$type.
'"', E_USER_WARNING);
142 function var_unserialise($str)
144 $str = str_replace(Array(
"\r\n",
"\r"),
"\n", $str);
145 $lines = explode(
"\n", trim($str));
147 list($var, $name) = _var_unserialise($lines, $i,
'');
162 function _var_unserialise(&$lines, &$i, $indent)
167 if ($str == $indent)
return Array(null, null);
172 $e =
'/^'.$indent.
'<name_type>(.*)<\/name_type><name>(.*)<\/name>(.*)$/';
173 if (preg_match($e, $str, $matches)) {
174 $name_type = $matches[1];
176 if ($name_type ==
'string') {
177 $name = str_replace($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $name);
180 settype($name, $name_type);
184 if ($str ==
'<val_type>array</val_type>'){
185 $indent_len = strlen($indent);
189 while (isset($lines[$i]) && $indent.
' ' == substr($lines[$i], 0, $indent_len + 1)) {
190 list($var, $key) = _var_unserialise($lines, $i, $indent.
' ');
196 return Array($val, $name);
203 $e =
'/^<val_type>(.*)<\/val_type><val>(.*)<\/val>$/';
204 if (preg_match($e, $str, $matches)) {
206 $val_type = $matches[1];
208 settype($val, $val_type);
211 if ($val_type ==
'string') {
212 $val = str_replace($GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $GLOBALS[
'VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $val);
217 return Array($val, $name);