Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript "serialize"

    Is there a function like serialize in php ?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there isn't one as far as I can tell. however, you could always use this WDDX method.

    also, JS is a weakly typed language, so can't really see the point. what you could do, though, is create a new array and add it to it, something like this:

    Code:
    (serial=[]).push(variable);
    *keep it simple (TM)

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could always use JSRS or XHR to communicate with PHP and use that to serialize/deserialize the data. that might be the best method.
    *keep it simple (TM)

  • #4
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guy , but using WDDX is not fully support in PHP (to my knowledge , it's only supported on Win32 OS , is it right ?) , i will try XHR

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What exactly are you wanting to do with your data? (And what kind of data?)

    You might be able to adapt the script from this thread, which dumps arrays into javascript-source strings.

    Or this start to a serialize function.

  • #6
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to pass data between PHP and Js .

    Using XHR or JSCR is too large . So i intend to save the serialized string to cookie , and PHP will unserialize the cookie to get the data (i don't want to pass data in URL because , there will be limited , right ? )

    Can you help me more plz !

  • #7
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another question :

    Did anyone write a function to unserialize PHP String to Javascript Variable ?

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by quocbao
    Did anyone write a function to unserialize PHP String to Javascript Variable ?
    why don't you google and find out for yourself ...
    *keep it simple (TM)

  • #9
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    0
    Thanks
    0
    Thanked 0 Times in 0 Posts

    a best PHP serialize/unserialize implementation for javascript

    Here is a best PHP serialize/unserialize implementation for javascript.

    It can serialize/unserialize N,b,i,d,s,U,r,R,a,O,C.

    It is included in PHPRPC

  • #10
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know this is an old thread, but I made a function to serialize a javascript array in a way php can unserialize it.

    Code:
    function serializeArray(a)
    {
    	var serializedString = '';
    	var arrayLength = 0;
    	for(var aKey in a)
    	{
    		//key definition
    		if(aKey * 1 == aKey) //is_numeric?
    		{
    			//integer keys look like i:key
    			serializedString += 'i:' + aKey + ';';	
    		}
    		else
    		{
    			//string keys look like s:key_length:key;
    			serializedString += 's:' + aKey.length + ':"' + aKey + '";';
    		}
    		
    		//value definition
    		if(a[aKey] * 1 == a[aKey])
    		{
    			//integer value look like i:value
    			serializedString += 'i:' + a[aKey] + ';';	
    		}
    		else if(typeof(a[aKey]) == "string")
    		{
    			//string value look like s:key_length:value;
    			serializedString += 's:' + a[aKey].length + ':"' + a[aKey] + '";';
    		}
    		else if(a[aKey] instanceof Array)
    		{
    			serializedString += serializeArray(a[aKey]);
    		}
    		arrayLength++;
    	}
    	serializedString = 'a:' + arrayLength + ':{' + serializedString + '}';
    	
    	return serializedString;
    }
    - Parris


  •  

    Posting Permissions

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