UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

427 lines (331 loc) 13.8 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://itech.hubpages.com" rel="nofollow">Krrish</a> </strong> on 2011-09-13 13:28:56 <br /> This is the Limit... Excellent work... You Guys converted nearly all PHP functions into JavaScript... You Got another Fan for your site and for your work. <hr /> <strong> Nilesh </strong> on 2010-10-08 07:36:20 <br /> why is the use of print_r functaion in php <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-02-03 04:29:00 <br /> It can print out a JavaScript object. Since JavaScript has no special associative arrays, this project treats JavaScript objects like associative arrays (that's most likely why it says &amp;quot;array&amp;quot; when you put in a JS object)--and since PHP lets you put in arrays (and objects too), this function should too. var_export() is another option for you too. I hope we can also get var_dump() added at some point, but you should be able to see the contents of your JS objects with print_r or var_export()... <hr /> <strong> <a href="www.ddddesigns.com" rel="nofollow">Dennis Day</a> </strong> on 2009-02-03 00:34:18 <br /> This code works great for printing out variable information but do you think it would be possible to print out a javascript object? I am new to javascript objects otherwise I would do it myself. To be honest, I'm not even sure it is possible. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-31 13:26:00 <br /> @ alexandre: I think your words are very clear :) Thanks alexandre, it's nice of you to let us know. Happy NYE tonight! <hr /> <strong> alexandre </strong> on 2008-12-31 02:36:32 <br /> no words are able to tell how wonderful is your work. Congratulations you all!!! PS: Sorry about my awful English... I'm Brazillian <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-01 09:17:16 <br /> @ nikdo: regular for loops won't work for associative arrays (js objects) so we really need these 'for key in array' structures. What version of ie are you using? I find it hard to believe that IE in general does not support these kind of loops at all, because that would have led to problems earlier on. <hr /> <strong> nikdo </strong> on 2008-11-28 12:48:13 <br /> using for(var a in b) isnt a good idea as it doesnt work in ie and is replacable with regular for(e1;e2;e3) <hr /> <strong> Alexander </strong> on 2008-11-19 15:53:51 <br /> May be need change: <pre><code> if (obj[key] instanceof Array) { </code></pre> Change to: <pre><code> if (obj[key] instanceof Array || obj[key] instanceof Object) { </code></pre> therefor print a nested objects <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-10-06 12:47:30 <br /> @ Tomot: Thanks for sharing, can you tell if it produces errors on any line? <hr /> <strong> Tomot </strong> on 2008-10-03 15:45:51 <br /> print_r(document, true) returns in IE7: &amp;quot;[object]&amp;quot; in FF3 works fine. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-09-29 12:33:29 <br /> @ Francois: The bug was in the htmlspecialchars_decode function and has been fixed. Sorry for the inconvenience and thank you for tipping me! <hr /> <strong> Francois </strong> on 2008-09-25 14:52:01 <br /> There is a missing coma (,) at line 2985 it is: = string.replace(/&amp;amp;gt;/g '&amp;gt;'); but we should read: = string.replace(/&amp;amp;gt;/g, '&amp;gt;'); <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-08-27 17:34:32 <br /> @aron budinszky: Thank you very much for your input. The object vs array story is because PHP does not differentiate between numerically indexed arrays and 'associative arrays'. But as soon as JavaScript encounters an associative array, it becomes an 'Object'. This is an essential difference between JS &amp;amp; PHP. In this project we've chosen to side with PHP. Hopefully this answers your question. As for the BR tag, if I output PHP's print_r I can choose to enclose it between PRE tags. I would like JS scripters to be able to approach JS's print_r in the same manner. <hr /> <strong> aron budinszky </strong> on 2008-08-07 10:47:49 <br /> also, changing the newlines to &amp;lt;br&amp;gt; and the pad_char to &amp;quot;&amp;amp;nbsp;&amp;quot; creates a more readable result, even if it deviates from the php norm. since print_r is normally used to visually represent data, and since javascript is typically run within a browser, this might be the preferred way in this case...of course the opposite can also be argued... <hr /> <strong> aron budinszky </strong> on 2008-08-07 09:27:32 <br /> oops. there was a mistake in my code below. here's the improved version...(using ben bryan's code). but i noticed that the line 6: if (obj[key] instanceof Array || obj[key] instanceof Object) is omitted from the current version in php.js, even though it is included in ben bryan's posted version... <pre><code> if (obj instanceof Array || obj instanceof Object) { if(obj instanceof Array) name = &amp;quot;Array&amp;quot;; else name = &amp;quot;Object&amp;quot;; str += name+&amp;quot;\n&amp;quot; + base_pad + &amp;quot;(\n&amp;quot;; for (var key in obj) { if (obj[key] instanceof Array || obj[key] instanceof Object) { str += thick_pad + &amp;quot;[&amp;quot;+key+&amp;quot;] =&amp;gt; &amp;quot;+formatArray(obj[key], cur_depth+1, pad_val, pad_char); } else { str += thick_pad + &amp;quot;[&amp;quot;+key+&amp;quot;] =&amp;gt; &amp;quot; + obj[key] + &amp;quot;\n&amp;quot;; } } str += base_pad + &amp;quot;)\n&amp;quot;; } else { str = obj.toString(); }; </code></pre> <hr /> <strong> aron budinszky </strong> on 2008-08-07 09:17:48 <br /> here's a bit of an improvement for the print_r function (which, as it seems, ben bryan has partially posted below) to process nested objects as well as arrays. in addition to his code, it is important to print OBJECT or to print ARRAY for the appropriate data type, since accessing the data requires slightly different syntax. <pre><code> // {{{ print_r function print_r( array, return_val ) { // Prints human-readable information about a variable // // + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_print_r/ // + version: 803.612 // + original by: Michael White (http://crestidg.com) // * example 1: print_r(['Kevin', 'van', 'Zonneveld']); // * returns 1: true var output = &amp;quot;&amp;quot;, pad_char = &amp;quot; &amp;quot;, pad_val = 4; var formatArray = function (obj, cur_depth, pad_val, pad_char) { if(cur_depth &amp;gt; 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 = &amp;quot;&amp;quot;; var name = &amp;quot;&amp;quot;; if(obj instanceof Array || obj instanceof Object) { if(obj instanceof Array) name = &amp;quot;Array&amp;quot;; else name = &amp;quot;Object&amp;quot;; str += name+&amp;quot;\n&amp;quot; + base_pad + &amp;quot;(\n&amp;quot;; for(var key in obj) { if(obj[key] instanceof Array || obj instanceof Object) { str += thick_pad + &amp;quot;[&amp;quot;+key+&amp;quot;] =&amp;gt; &amp;quot;+formatArray(obj[key], cur_depth+1, pad_val, pad_char); } else { str += thick_pad + &amp;quot;[&amp;quot;+key+&amp;quot;] =&amp;gt; &amp;quot; + obj[key] + &amp;quot;\n&amp;quot;; } } str += base_pad + &amp;quot;)\n&amp;quot;; } else { str = obj.toString(); }; return str; }; var repeat_char = function (len, char) { var str = &amp;quot;&amp;quot;; for(var i=0; i &amp;lt; len; i++) { str += char; }; return str; }; output = formatArray(array, 0, pad_val, pad_char); if(return_val !== true) { document.write(&amp;quot;&amp;lt;pre&amp;gt;&amp;quot; + output + &amp;quot;&amp;lt;/pre&amp;gt;&amp;quot;); return true; } else { return output; } }// }}} </code></pre> <hr /> <strong> Alejandro </strong> on 2008-06-04 17:14:28 <br /> Excelent!!!!!!!!!!! <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-05-31 14:36:14 <br /> @ vinnieboombots: Looking at the code, I cannot establish how that could have happened at the moment. Would you be able to provide more debug info? (maybe a codeblock with how you tried to run print_r exactly?) Thank you. <hr /> <strong> vinnieboombots </strong> on 2008-05-26 17:49:46 <br /> used it on an array of all links on this page (firebug) it replaced the page contents with a comma separated value string of the array's values. ie: http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog Php's print_r output is different (&amp;amp; better) array [0]=&amp;gt;http://kevin.vanzonneveld.net/techblog [1]=&amp;gt;http://kevin.vanzonneveld.net/links [2]=&amp;gt;http://kevin.vanzonneveld.net/code [3]=&amp;gt;http://kevin.vanzonneveld.net/about <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-05-20 23:27:56 <br /> @ Ben Bryan: Thanks a lot Ben! <hr /> <strong> Ben Bryan </strong> on 2008-05-20 06:00:20 <br /> Where formatArray function is testing data types, it does not except types of Object, replacing the if statement with the following seems to overcome this. Basically letting OBject types be processed. Tested with FF. <pre><code> if (obj instanceof Array || obj instanceof Object) { str += &amp;quot;Array\n&amp;quot; + base_pad + &amp;quot;(\n&amp;quot;; for (var key in obj) { if (obj[key] instanceof Array || obj[key] instanceof Object) { str += thick_pad + &amp;quot;[&amp;quot;+key+&amp;quot;] =&amp;gt; &amp;quot;+formatArray(obj[key], cur_depth+1, pad_val, pad_char); } else { str += thick_pad + &amp;quot;[&amp;quot;+key+&amp;quot;] =&amp;gt; &amp;quot; + obj[key] + &amp;quot;\n&amp;quot;; } } str += base_pad + &amp;quot;)\n&amp;quot;; } else { str = obj.toString(); }; </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-05-08 22:01:40 <br /> @ Günter Kits: I do not agree. When the return parameter is not true, the function should not return the string but print it instead. Unless I'm overlooking something, that's what it does now? <hr /> <strong> Günter Kits </strong> on 2008-05-08 14:27:04 <br /> <pre><code>if(return_val !== true) {</code></pre> should be <pre><code>if(return_val == true) {</code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-15 11:55:34 <br /> @ Michael White: replaced! <hr /> <strong> Michael White </strong> on 2008-03-11 21:03:15 <br /> Found a bug in print_r() So far this bug only seems to affect Netscape and the version I am using is 7.2 Replace this segment: <pre><code> var repeat_char = function (len, char) { var str = &amp;quot;&amp;quot;; for(var i=0; i &amp;lt; len; i++) { str += char; }; return str; }; </code></pre> with this segment: <pre><code> var repeat_char = function (len, pad_char) { var str = &amp;quot;&amp;quot;; for(var i=0; i &amp;lt; len; i++) { str += pad_char; }; return str; }; </code></pre> Netscape thinks that &amp;quot;char&amp;quot; is a reserved word and so cannot be used as a variable name. It errors out saying something about a formal parameter. Changing the 'char&amp;quot; variable to &amp;quot;pad_char&amp;quot; solves that quite easily. http://crestidg.com <hr /> <strong> ricardo avalos </strong> on 2008-03-11 16:17:45 <br /> Greats Work!! Very, very, very thanks you!!! All my cordial greetings and appreciation from Chile Ricardo avalos.ricardo@gmail.com <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-04 23:35:42 <br /> @ Alfonso Jiménez: Great contribution, thank you very much! <hr /> <strong> Alfonso Jiménez </strong> on 2008-03-04 23:15:33 <br /> Hi Kevin! I post here the array_reduce function: <pre><code> function array_reduce(a_input, callback) { var lon = a_input.length; var res = 0; var tmp = new Array(); for(i = 0; i &amp;lt; lon; i += 2) { tmp[0] = a_input[i]; if(a_input[i+1]) tmp[1] = a_input[i+1]; else tmp[1] = 0; res += callback.apply(null, tmp); tmp = new Array(); } return res; } </code></pre> Usage example: <pre><code> array_reduce([1,2,3,4,5], function (x, y) { return (x+y); }); </code></pre> Regards! Alfonso Jiménez (http://www.alfonsojimenez.com) <hr />