UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

671 lines (534 loc) 12.5 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://www.coursesweb.net" rel="nofollow">CoursesWeb</a> </strong> on 2012-04-30 08:07:26 <br /> Hi, For is_numeric I use this: <pre><code> return /^[0-9]+[\.,]{0,1}[0-9]*$/i.test(obj); </code></pre> <hr /> <strong> max4ever </strong> on 2011-10-13 12:17:12 <br /> please consider this function http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844 <hr /> <strong> max4ever </strong> on 2011-10-13 12:08:35 <br /> isNumeric('3a') ==&gt; return true, should return false <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-04-03 16:44:35 <br /> @ Tim de Koning: Thank you for noticing. I had to fix it a bit differently in but the bottom line is your testcase works now. Thanks! <hr /> <strong> <a href="http://www.kingsquare.nl" rel="nofollow">Tim de Koning</a> </strong> on 2009-03-31 16:28:55 <br /> Hi Kevin e.a. is_numeric('') returns true in javascript, not in PHP... Shouldn't this be: <pre><code> function is_numeric( mixed_var ) { return !isNaN(parseInt(mixed_var)); } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-02 23:35:35 <br /> @ taith: Check, fixed! <hr /> <strong> <a href="divinedesigns.ca" rel="nofollow">taith</a> </strong> on 2009-02-02 15:07:02 <br /> some browsers will interpret a number as a string depending on how its set... hence a number, can be defined as a string, making the function return false all the time... this will automatically turn it into an integer in this case <pre><code> function is_numeric(integer){ return (!isNaN(integer*1)); } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-01 15:36:56 <br /> @ Martijn Wieringa: And additional compliments for solid code. The integration went seamlessly, nice job! <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-01 15:17:51 <br /> @ Martijn Wieringa: I will add the functions that are missing here, thanks alot man! <hr /> <strong> Martijn Wieringa </strong> on 2008-03-01 13:56:57 <br /> I've been working on a simular project.. Here are some functions i completes so far. <pre><code> // Load PHP library var PHP = new PHP_LIBRARY(); // Call some function within PHP library PHP.func(params); </code></pre> Here's my library (so far) <pre><code> var PHP_LIBRARY = function() {} PHP_LIBRARY.prototype = { 'abs' : function(f_float) { return isNaN(f_float) ? 0 : Math.abs(f_float); }, 'chr' : function(f_ascii) { return String.fromCharCode(f_ascii); }, 'explode' : function(f_seperator, f_string) { return f_string.split(f_seperator); }, 'implode' : function(f_glue, f_array) { return f_array.join(f_glue); }, 'join' : function(f_glue, f_array) { return this.implode(f_glue, f_array); }, 'number_format' : function(f_float, f_decimals, f_decimal_sign, f_thousand_sign) { if(f_decimals == undefined) { f_decimals = 0; } if(f_decimal_sign == undefined) { f_decimal_sign = ''; } if(f_thousand_sign == undefined) { f_thousand_sign = ''; } var result = this.implode(f_thousand_sign, this.str_split(Math.floor(f_float).toString(), 3, true)); if(f_decimals &amp;gt; 0) { var d = Math.round((f_float % 1) * Math.pow(10, f_decimals)).toString(); result += f_decimal_sign + d + this.str_repeat('0', f_decimals - d.length); } return result; }, 'ord' : function(f_string) { return f_string.charCodeAt(0); }, 'split' : function(f_seperator, f_string) { return this.explode(f_seperator, f_string); }, 'str_repeat' : function(f_string, f_repeat) { var result = ''; while(f_repeat &amp;gt; 0) { result += f_string; f_repeat--; } return result; }, 'str_replace' : function(f_needle, f_replace, f_haystack) { var result = ''; var index = 0; while((index = f_haystack.indexOf(f_needle)) &amp;gt; -1) { result += f_haystack.substring(0, index); result += f_replace; f_haystack = f_haystack.substring(index + f_needle.length); } return result + f_haystack; }, 'str_ireplace' : function(f_needle, f_replace, f_haystack) { var result = ''; var index = 0; var haystack = f_haystack.toLowerCase(); var needle = f_needle.toLowerCase(); while((index = haystack.indexOf(needle)) &amp;gt; -1) { result += f_haystack.substring(0, index); result += f_replace; haystack = haystack.substring(index + f_needle.length); f_haystack = f_haystack.substring(index + f_needle.length); } return result + f_haystack; }, 'str_split' : function(f_string, f_split_length, f_backwards) { if(f_backwards == undefined) { f_backwards = false; } if(f_split_length &amp;gt; 0) { var result = new Array(); if(f_backwards) { var r = (f_string.length % f_split_length); if(r &amp;gt; 0) { result[result.length] = f_string.substring(0, r); f_string = f_string.substring(r); } } while(f_string.length &amp;gt; f_split_length) { result[result.length] = f_string.substring(0, f_split_length); f_string = f_string.substring(f_split_length); } result[result.length] = f_string; return result; } return false; }, 'strcasecmp' : function(f_string1, f_string2) { var string1 = f_string1.toLowerCase(); var string2 = f_string2.toLowerCase(); if(string1 &amp;gt; string2) { return 1; } else if(string1 == string2) { return 0; } return -1; }, 'strcmp' : function(f_string1, f_string2) { if(f_string1 &amp;gt; f_string2) { return 1; } else if(f_string1 == f_string2) { return 0; } return -1; }, 'stripos' : function(f_haystack, f_needle, f_offset) { var haystack = f_haystack.toLowerCase(); var needle = f_needle.toLowerCase(); var index = 0; if(f_offset == undefined) { f_offset = 0; } if((index = haystack.indexOf(needle, f_offset)) &amp;gt; -1) { return index; } return false; }, 'strlen' : function(f_string) { return f_string.length; }, 'strnatcasecmp' : function(f_string1, f_string2, f_version) { this.strnatcmp(f_string1.toLowerCase(), f_string2.toLowerCase(), f_version); }, 'strnatcmp' : function(f_string1, f_string2) { if(f_version == undefined) { f_version = false; } var array1 = this.__strnatcmp_split(f_string1); var array2 = this.__strnatcmp_split(f_string2); var len = array1.length; var text = true; var result = -1; var r = 0; if(len &amp;gt; array2.length) { len = array2.length; result = 1; } for(i = 0; i &amp;lt; len; i++) { if(isNaN(array1[i])) { if(isNaN(array2[i])) { text = true; if((r = this.strcmp(array1[i], array2[i])) != 0) { return r; } } else if(text) { return 1; } else { return -1; } } else if(isNaN(array2[i])) { if(text) { return -1; } else { return 1; } } else { if(text || f_version) { if((r = (array1[i] - array2[i])) != 0) { return r; } } else { if((r = this.strcmp(array1[i].toString(), array2[i].toString())) != 0) { return r; } } text = false; } } return result; }, '__strnatcmp_split' : function(f_string) { var result = new Array(); var buffer = ''; var chr = ''; var text = true; for(var i = 0; i &amp;lt; f_string.length; i++) { chr = f_string.substring(i, i + 1); if(chr.match(/[0-9]/)) { if(text) { if(buffer.length &amp;gt; 0) { result[result.length] = buffer; buffer = ''; } text = false; } buffer += chr; } else if((text == false) &amp;amp;&amp;amp; (chr == '.') &amp;amp;&amp;amp; (i &amp;lt; (f_string.length - 1)) &amp;amp;&amp;amp; (f_string.substring(i + 1, i + 2).match(/[0-9]/))) { result[result.length] = buffer; buffer = ''; } else { if(text == false) { if(buffer.length &amp;gt; 0) { result[result.length] = parseInt(buffer); buffer = ''; } text = true; } buffer += chr; } } if(buffer.length &amp;gt; 0) { if(text) { result[result.length] = buffer; } else { result[result.length] = parseInt(buffer); } } return result; }, 'strpos' : function(f_haystack, f_needle, f_offset) { var index = 0; if(f_offset == undefined) { f_offset = 0; } if((index = f_haystack.indexOf(f_needle, f_offset)) &amp;gt; -1) { return index; } return false; }, 'strrev' : function(f_string) { var result = ''; var index = f_string.length - 1; while(index &amp;gt;= 0) { result += f_string.substring(index, index + 1); index--; } return result; }, 'strripos' : function(f_haystack, f_needle, f_offset) { var haystack = f_haystack.toLowerCase(); var needle = f_needle.toLowerCase(); var index = 0; if((index = haystack.indexOf(needle, f_offset)) &amp;gt; -1) { do { f_offset = index; } while((index = haystack.indexOf(needle, f_offset + 1)) &amp;gt; -1); return f_offset; } return false; }, 'strrpos' : function(f_haystack, f_needle, f_offset) { var index = 0; if((index = f_haystack.indexOf(f_needle, f_offset)) &amp;gt; -1) { do { f_offset = index; } while((index = f_haystack.indexOf(f_needle, f_offset + 1)) &amp;gt; -1); return f_offset; } return false; }, 'strtolower' : function(f_string) { return f_string.toLowerCase(); }, 'strtoupper' : function(f_string) { return f_string.toUpperCase(); }, 'substr' : function(f_string, f_start, f_length) { if(f_start &amp;lt; 0) { f_start += f_string.length; } if(f_length == undefined) { f_length = f_string.length; } else if(f_length &amp;lt; 0) { f_length += f_string.length; } else { f_length += f_start; } if(f_length &amp;lt; f_start) { f_length = f_start; } return f_string.substring(f_start, f_length); }, 'substr_count' : function(f_haystack, f_needle, f_offset) { var result = 0; var index = 0; if(f_offset == undefined) { f_offset = 0; } while((index = f_haystack.indexOf(f_needle, f_offset + 1)) &amp;gt; -1) { result++; f_offset = index; } return result; }, 'trim' : function(f_string) { return f_string.replace(/^\s*/, '').replace(/\s*$/, ''); }, 'ucfirst' : function(f_string) { return f_string.substring(0, 1).toUpperCase() + f_string.substring(1); }, 'ucword' : function(f_string) { var result = ''; var chr = ''; var swap = true; for(var i = 0; i &amp;lt; f_string.length; i++) { chr = f_string.substring(i, i + 1); if(swap) { result += chr.toUpperCase(); } else { result += chr; } if(chr.match(/\s/)) { swap = true; } else { swap = false; } } return result; } } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-16 22:17:44 <br /> @ David: Thank you for noticing. I thouhgt of isNaN(), which I think does the trick. If you wan't to be credited differently let me know. <hr /> <strong> David </strong> on 2008-01-16 20:10:36 <br /> One of your tests should be: is_numeric(&amp;quot;+186.31e2&amp;quot;); And that needs to return true. <hr /> <strong> David </strong> on 2008-01-16 20:08:49 <br /> The is_numeric function is not correct, at least it doesn't work like PHP. A numeric string, like &amp;quot;-876.20&amp;quot; should return true, but it doesn't because it doesn't pass the [typeof mixed_var == 'number'] condition. <hr />