Implementarea functiei PHP serialize() in JavaScript

JavaScript:
  1. /**
  2. *function implementing PHP's serialize() function
  3. *@param: v - elements to be serialized
  4. *@return: serialized string
  5. */
  6. function serialize(v){
  7. if(typeof(v)=='object' && v.constructor==Array){
  8. var i,s='',c=0;
  9. for(i in v){
  10. ++c;
  11. s+=serialize(i)+serialize(v[i]);
  12. }
  13. s="a:"+c+":{"+s+"}";
  14. return s;
  15. } else {
  16. if(Number(v)==v){
  17. return 'i:'+v+';';
  18. } else
  19. if(typeof(v)=='string'){
  20. return 's:'+v.length+':"'+v+'";';
  21. }
  22. }
  23. }

Bookmark and Share:

Leave a Reply


Comments links could be nofollow free.
Security Code: