/_octopress/source/functions/print_r/index.markdown

https://gitlab.com/orvi2014/phpjs · Markdown · 111 lines · 98 code · 13 blank · 0 comment · 0 complexity · 27b5e1c180b5dbb7adab53d74b248179 MD5 · raw file

  1. ---
  2. layout: page
  3. title: "JavaScript print_r function"
  4. comments: true
  5. sharing: true
  6. footer: true
  7. alias:
  8. - /functions/view/print_r:493
  9. - /functions/view/print_r
  10. - /functions/view/493
  11. - /functions/print_r:493
  12. - /functions/493
  13. ---
  14. <!-- Generated by Rakefile:build -->
  15. A JavaScript equivalent of PHP's print_r
  16. {% codeblock var/print_r.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/print_r.js raw on github %}
  17. function print_r(array, return_val) {
  18. // discuss at: http://phpjs.org/functions/print_r/
  19. // http: //kevin.vanzonneveld.net
  20. // original by: Michael White (http://getsprink.com)
  21. // improved by: Ben Bryan
  22. // improved by: Brett Zamir (http://brett-zamir.me)
  23. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  24. // input by: Brett Zamir (http://brett-zamir.me)
  25. // depends on: echo
  26. // example 1: print_r(1, true);
  27. // returns 1: 1
  28. var output = '',
  29. pad_char = ' ',
  30. pad_val = 4,
  31. d = this.window.document,
  32. getFuncName = function(fn) {
  33. var name = (/\W*function\s+([\w\$]+)\s*\(/)
  34. .exec(fn);
  35. if (!name) {
  36. return '(Anonymous)';
  37. }
  38. return name[1];
  39. };
  40. repeat_char = function(len, pad_char) {
  41. var str = '';
  42. for (var i = 0; i < len; i++) {
  43. str += pad_char;
  44. }
  45. return str;
  46. };
  47. formatArray = function(obj, cur_depth, pad_val, pad_char) {
  48. if (cur_depth > 0) {
  49. cur_depth++;
  50. }
  51. var base_pad = repeat_char(pad_val * cur_depth, pad_char);
  52. var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
  53. var str = '';
  54. if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !==
  55. 'PHPJS_Resource') {
  56. str += 'Array\n' + base_pad + '(\n';
  57. for (var key in obj) {
  58. if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
  59. str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
  60. } else {
  61. str += thick_pad + '[' + key + '] => ' + obj[key] + '\n';
  62. }
  63. }
  64. str += base_pad + ')\n';
  65. } else if (obj === null || obj === undefined) {
  66. str = '';
  67. } else { // for our "resource" class
  68. str = obj.toString();
  69. }
  70. return str;
  71. };
  72. output = formatArray(array, 0, pad_val, pad_char);
  73. if (return_val !== true) {
  74. if (d.body) {
  75. this.echo(output);
  76. } else {
  77. try {
  78. d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
  79. this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</pre>');
  80. } catch (e) {
  81. this.echo(output); // Outputting as plain text may work in some plain XML
  82. }
  83. }
  84. return true;
  85. }
  86. return output;
  87. }
  88. {% endcodeblock %}
  89. - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/print_r.js)
  90. Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
  91. the closest match to this hashtable-like data structure.
  92. Please also note that php.js offers community built functions and goes by the
  93. [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
  94. functions that are far from perfect, in the hopes to spark better contributions.
  95. Do you have one? Then please just:
  96. - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/print_r.js)
  97. ### Other PHP functions in the var extension
  98. {% render_partial _includes/custom/var.html %}