SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript "serialize"

    Hi , i've written a js function which will convert data to php serialize string

    Code:
    <script language="javascript">
    function php_serialize(s) {
    	switch (typeof(s)) {
    	case "string":
    		s=s.replace(/\\/g, "\\\\")
    		s=s.replace(/\"/g, "\\\"")
    		s=s.replace(/\n/g, "\\n")
    		s=s.replace(/\r/g, "")
    		s='s:'+s.length+':"'+s+'";';
    	break;
    	case "number":
    		if (s.toString().indexOf(".") != -1) s = "i:" + s + ";";
    		else s = "d:" + s + ";";
    	break;
    	case "object":
    		var l = 0;
    		var mys = "";
    		for (key in s) {
    			l += 1;
    			mys += php_serialize(key) + php_serialize(s[key]);
    		}
    		mys = "a:" + l + ":{" + mys + "}";
    		s = mys;
    	break;
    	}
    	return s;
    }
    
    vars = new Array;
    vars["a"] = "Test";
    vars["b"] = "Test1";
    vars["c"] = new Array;
    vars['d'] = "Test3";
    
    document.write(php_serialize(vars));
    
    </script>
    I've tested it with PHP , and it worked well . But it doesn't support object , anyone help me with it plz

  2. #2
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Er...seems to work for me. Output:

    Code:
    a:4:{s:1:"a";s:4:"Test";s:1:"b";s:5:"Test1";s:1:"c";a:0:{}s:1:"d";s:5:"Test3";}

  3. #3
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can this be of any help????


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •