Costinu’s Blog

When you dream, there are no rules. People can fly; anything can happen. Sometimes, there’s a moment as you’re waking, and you become aware of the real world around you; but you’re still dreaming. You may think you can fly, but you better not try.
Oct 5
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. }

Oct 5
JavaScript:
  1. /**
  2. *function : print_r()
  3. *@param: array-array,hass or object
  4. *@param: level - OPTIONAL
  5. *@returns: string - The textual representation of the array.
  6. */
  7. function print_r(array,level) {
  8. var dumped_text = "";
  9. if(!level) level = 0;
  10. //The padding given at the beginning of the line.
  11. var level_padding = "   ";
  12. for(var j=0;j<level;j++) level_padding += "    ";
  13. if(typeof(array) == 'object') { //Array/Hashes/Objects
  14. var obj = 0;
  15. for(var item in array) {
  16. var value = array[item];
  17. if(typeof(value) == 'object') { //If it is an array,
  18. dumped_text += level_padding + "[" + item + "] => Array\n " + level_padding + "( \n";
  19. dumped_text += print_r(value,level+1);
  20. dumped_text += level_padding + ")\n";
  21. } else {
  22. dumped_text += level_padding + level_padding +  "[" + item + "] => " + value + "\n";
  23. }
  24. }
  25. } else { //Stings/Chars/Numbers etc.
  26. dumped_text = "===>"+array+"<===("+typeof(array)+")";
  27. }
  28. return dumped_text;
  29. }
  30. /**
  31. *call of function
  32. */
  33. alert (print_r (MyArray));