/_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
- ---
- layout: page
- title: "JavaScript print_r function"
- comments: true
- sharing: true
- footer: true
- alias:
- - /functions/view/print_r:493
- - /functions/view/print_r
- - /functions/view/493
- - /functions/print_r:493
- - /functions/493
- ---
- <!-- Generated by Rakefile:build -->
- A JavaScript equivalent of PHP's print_r
- {% codeblock var/print_r.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/print_r.js raw on github %}
- function print_r(array, return_val) {
- // discuss at: http://phpjs.org/functions/print_r/
- // http: //kevin.vanzonneveld.net
- // original by: Michael White (http://getsprink.com)
- // improved by: Ben Bryan
- // improved by: Brett Zamir (http://brett-zamir.me)
- // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // input by: Brett Zamir (http://brett-zamir.me)
- // depends on: echo
- // example 1: print_r(1, true);
- // returns 1: 1
- var output = '',
- pad_char = ' ',
- pad_val = 4,
- d = this.window.document,
- getFuncName = function(fn) {
- var name = (/\W*function\s+([\w\$]+)\s*\(/)
- .exec(fn);
- if (!name) {
- return '(Anonymous)';
- }
- return name[1];
- };
- repeat_char = function(len, pad_char) {
- var str = '';
- for (var i = 0; i < len; i++) {
- str += pad_char;
- }
- return str;
- };
- formatArray = function(obj, cur_depth, pad_val, pad_char) {
- if (cur_depth > 0) {
- cur_depth++;
- }
- var base_pad = repeat_char(pad_val * cur_depth, pad_char);
- var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
- var str = '';
- if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !==
- 'PHPJS_Resource') {
- str += 'Array\n' + base_pad + '(\n';
- for (var key in obj) {
- if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
- str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
- } else {
- str += thick_pad + '[' + key + '] => ' + obj[key] + '\n';
- }
- }
- str += base_pad + ')\n';
- } else if (obj === null || obj === undefined) {
- str = '';
- } else { // for our "resource" class
- str = obj.toString();
- }
- return str;
- };
- output = formatArray(array, 0, pad_val, pad_char);
- if (return_val !== true) {
- if (d.body) {
- this.echo(output);
- } else {
- try {
- d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
- this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</pre>');
- } catch (e) {
- this.echo(output); // Outputting as plain text may work in some plain XML
- }
- }
- return true;
- }
- return output;
- }
- {% endcodeblock %}
- - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/print_r.js)
- Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
- the closest match to this hashtable-like data structure.
- Please also note that php.js offers community built functions and goes by the
- [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
- functions that are far from perfect, in the hopes to spark better contributions.
- Do you have one? Then please just:
- - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/print_r.js)
- ### Other PHP functions in the var extension
- {% render_partial _includes/custom/var.html %}