HTML_AJAX
[ class tree: HTML_AJAX ] [ index: HTML_AJAX ] [ all elements ]

Source for file serialize.examples.php

Documentation is available at serialize.examples.php

  1. <script type="text/javascript" src="serialize.js"></script>
  2. <script type="text/javascript">
  3.  
  4. function describe(inp)
  5. {
  6.     var type = gettype(inp);
  7.     var ctype;
  8.     var out = "Type: " + type + "\nContents: ";
  9.     switch (type) {
  10.         case "string":
  11.         case "number":
  12.             out += inp;
  13.             break;
  14.         case "boolean":
  15.             out += (inp ? 'true' : 'false');
  16.             break;
  17.         case "array":
  18.         case "object":
  19.             for (k in inp) {
  20.                 out += k + " => ";
  21.                 ctype = gettype(inp[k]);
  22.                 out += (ctype == "array" || ctype == "object" ? describe(inp[k]) : inp[k]) + "\n";
  23.             }
  24.     }
  25.     return out;
  26. }
  27.  
  28. unserializer = new Unserializer;
  29.  
  30. </script>
  31. <?php
  32.  
  33. $examples = array(
  34.     '$foo = null;',
  35.     '$foo = true;',
  36.     '$foo = "foobar";',
  37.     '$foo = 337;',
  38.     '$foo = 99.99;',
  39.     '$foo = array("a" => 1, "b" => 2, 3);',
  40.     '$foo = array(1,2,array(1,2,3));',
  41.     'class Foo { var $foo; var $bar; }' 
  42.     . '$foo = new Foo; $foo->foo = "hello"; $foo->bar = array("world","universe");'
  43. );
  44.  
  45. echo '<h1><a name="pos">Positives</a> | <a href="#neg">Negatives</a></h1>';
  46. $c count($examples);
  47. for ($i = 0; $i $c$i++{
  48.     echo "<strong>PHP Code:</strong>\n<pre>$examples[$i]</pre>\n<strong>PHP value:</strong><pre>\n";
  49.     eval($examples[$i]);
  50.     var_dump($foo);
  51.     $sfoo serialize($foo);
  52.     echo "</pre>\n<strong>Serialized in PHP:</strong>\n<pre>"$sfoo"</pre>\n",
  53.          "<strong>Unserialized in JS:</strong>\n<pre>\n",
  54.          '<script type="text/javascript">',
  55.          'var sfoo = unescape("'urlencode($sfoo)'"); var usfoo = unserializer.unserialize(sfoo); if (unserializer.error) {',
  56.          'document.write("Error: " + unserializer.getError() + "\n"); } document.write(describe(usfoo) + ',
  57.          '"</pre>\n<strong>Serialized in JS:</strong>\n<pre>" + serialize(usfoo));'"</script>\n</pre>\n<hr />\n\n";
  58. }
  59.  
  60. $bad_examples = array(
  61.     'x',
  62.     'x:1',
  63.     'N',
  64.     'Nx',
  65.     'b:f;',
  66.     'b:1',
  67.     'i:foo;',
  68.     'i:1',
  69.     'd:foo;',
  70.     'd:1.1.1;',
  71.     'd:1.1',
  72.     's:6:"foo";',
  73.     's:6:"foofoo"',
  74.     's:1:"foo";',   
  75.     's:0:""',
  76.     'a:3:{s:1:"aa";i:1;s:1:"b";i:2;i:0;i:3;}',
  77.     'a:4:{s:1:"a";i:1;s:1:"b";i:2;i:0;i:3;',
  78.     'a:3:{i:1;s:1:"b";i:2;i:0;i:3;}',
  79.     'a:3:{}',
  80.     'O:3:"Fooo":2:{s:3:"foo";s:5:"hello";s:3:"bar";a:2:{i:0;s:5:"world";i:1;s:8:"universe";}}',
  81.     'O:3:"Foo":3:{s:3:"foo";s:5:"hello";s:3:"bar";a:2:{i:0;s:5:"world";i:1;s:8:"universe";}}',
  82.     'O:3:"Foo":2:{s:5:"hello";s:3:"bar";a:2:{i:0;s:5:"world";i:1;s:8:"universe";}}',
  83.     'O:3:"Foo":2:{s:3:"foo";s:5:"hello";s:3:"bar";a:2:{i:0;s:5:"world";i:1;s:8:"universe";}',
  84.     'O:3:"Foo":2:{s:3:"foo";s:5:"hello";s:3:"bar";a:2:{i:0;s:5:"world";i:1;s:8:"universe"}}'
  85. );
  86.  
  87. echo '<h1><a href="#pos">Positives</a> | <a name="neg">Negatives</a></h1>';
  88. foreach ($bad_examples as $sfoo{
  89.     echo "</pre>\n<strong>Invalidly serialized:</strong>\n<pre>"$sfoo"</pre>\n",
  90.          "<strong>Unserialized in JS:</strong>\n<pre>\n",
  91.          '<script type="text/javascript">',
  92.          'var sfoo = unescape("'urlencode($sfoo)'"); var usfoo = unserializer.unserialize(sfoo); if (unserializer.error) {',
  93.          'document.write("Error: " + unserializer.getError() + "\n"); } document.write(describe(usfoo));',
  94.          "</script>\n</pre>\n<hr />\n\n";
  95. }
  96.  
  97.  
  98. ?>

Documentation generated on Mon, 11 Mar 2019 14:11:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.