PageRenderTime 18ms CodeModel.GetById 14ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 3ms

/js/yii/vendors/phpjs/php.js

http://github.com/phpnode/YiiJS
JavaScript | 7274 lines | 4178 code | 533 blank | 2563 comment | 1166 complexity | f435a70ee00a32b18ecea0911100c6b9 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* 
   2 * More info at: http://phpjs.org
   3 * 
   4 * This is version: 3.24
   5 * php.js is copyright 2011 Kevin van Zonneveld.
   6 * 
   7 * Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
   8 * (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
   9 * (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jonas Raoni
  10 * Soares Silva (http://www.jsfromhell.com), Jack, Philip Peterson, Ates Goral
  11 * (http://magnetiq.com), Legaev Andrey, Ratheous, Alex, Martijn Wieringa,
  12 * Nate, lmeyrick (https://sourceforge.net/projects/bcmath-js/), Enrique
  13 * Gonzalez, Philippe Baumann, Rafał Kukawski (http://blog.kukawski.pl),
  14 * Webtoolkit.info (http://www.webtoolkit.info/), Ole Vrijenhoek, Ash Searle
  15 * (http://hexmen.com/blog/), travc, Carlos R. L. Rodrigues
  16 * (http://www.jsfromhell.com), Jani Hartikainen, stag019, GeekFG
  17 * (http://geekfg.blogspot.com), WebDevHobo (http://webdevhobo.blogspot.com/),
  18 * Erkekjetter, pilus, Rafał Kukawski (http://blog.kukawski.pl/), Johnny Mast
  19 * (http://www.phpvrouwen.nl), T.Wild,
  20 * http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
  21 * d3x, Michael Grier, Andrea Giammarchi (http://webreflection.blogspot.com),
  22 * marrtins, Mailfaker (http://www.weedem.fr/), Steve Hilder, gettimeofday,
  23 * mdsjack (http://www.mdsjack.bo.it), felix, majak, Steven Levithan
  24 * (http://blog.stevenlevithan.com), Mirek Slugen, Oleg Eremeev, Felix
  25 * Geisendoerfer (http://www.debuggable.com/felix), Martin
  26 * (http://www.erlenwiese.de/), gorthaur, Lars Fischer, Joris, AJ, Paul Smith,
  27 * Tim de Koning (http://www.kingsquare.nl), KELAN, Josh Fraser
  28 * (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
  29 * Chris, Marc Palau, Kevin van Zonneveld (http://kevin.vanzonneveld.net/),
  30 * Arpad Ray (mailto:arpad@php.net), Breaking Par Consulting Inc
  31 * (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
  32 * Nathan, Karol Kowalski, David, Dreamer, Diplom@t (http://difane.com/), Caio
  33 * Ariede (http://caioariede.com), Robin, Imgen Tata (http://www.myipdf.com/),
  34 * Pellentesque Malesuada, saulius, Aman Gupta, Sakimori, Tyler Akins
  35 * (http://rumkin.com), Thunder.m, Public Domain
  36 * (http://www.json.org/json2.js), Michael White, Kankrelune
  37 * (http://www.webfaktory.info/), Alfonso Jimenez
  38 * (http://www.alfonsojimenez.com), Frank Forte, vlado houba, Marco, Billy,
  39 * David James, madipta, noname, sankai, class_exists, Jalal Berrami, ger,
  40 * Itsacon (http://www.itsacon.net/), Scott Cariss, nobbler, Arno, Denny
  41 * Wardhana, ReverseSyntax, Mateusz "loonquawl" Zalega, Slawomir Kaniecki,
  42 * Francois, Fox, mktime, Douglas Crockford (http://javascript.crockford.com),
  43 * john (http://www.jd-tech.net), Oskar Larsson Högfeldt
  44 * (http://oskar-lh.name/), marc andreu, Nick Kolosov (http://sammy.ru), date,
  45 * Marc Jansen, Steve Clay, Olivier Louvignes (http://mg-crea.com/), Soren
  46 * Hansen, merabi, Subhasis Deb, josh, T0bsn, Tim Wiel, Brad Touesnard, MeEtc
  47 * (http://yass.meetcweb.com), Peter-Paul Koch
  48 * (http://www.quirksmode.org/js/beat.html), Pyerre, Jon Hohle, duncan, Bayron
  49 * Guevara, Adam Wallner (http://web2.bitbaro.hu/), paulo kuong, Gilbert,
  50 * Lincoln Ramsay, Thiago Mata (http://thiagomata.blog.com), Linuxworld,
  51 * lmeyrick (https://sourceforge.net/projects/bcmath-js/this.), djmix, Bryan
  52 * Elliott, David Randall, Sanjoy Roy, jmweb, Francesco, Stoyan Kyosev
  53 * (http://www.svest.org/), J A R, kenneth, T. Wild, Ole Vrijenhoek
  54 * (http://www.nervous.nl/), Raphael (Ao RUDLER), Shingo, LH, JB, nord_ua, jd,
  55 * JT, Thomas Beaucourt (http://www.webapp.fr), Ozh, XoraX
  56 * (http://www.xorax.info), EdorFaus, Eugene Bulkin (http://doubleaw.com/),
  57 * Der Simon (http://innerdom.sourceforge.net/), 0m3r, echo is bad,
  58 * FremyCompany, stensi, Kristof Coomans (SCK-CEN Belgian Nucleair Research
  59 * Centre), Devan Penner-Woelk, Pierre-Luc Paour, Martin Pool, Brant Messenger
  60 * (http://www.brantmessenger.com/), Kirk Strobeck, Saulo Vallory, Christoph,
  61 * Wagner B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
  62 * (http://carrot.org/), Daniel Esteban, strftime, Rick Waldron, Mick@el,
  63 * Anton Ongson, Bjorn Roesbeke (http://www.bjornroesbeke.be/), Simon Willison
  64 * (http://simonwillison.net), Gabriel Paderni, Philipp Lenssen, Marco van
  65 * Oort, Bug?, Blues (http://tech.bluesmoon.info/), Tomasz Wesolowski, rezna,
  66 * Eric Nagel, Evertjan Garretsen, Luke Godfrey, Pul, Bobby Drake, uestla,
  67 * Alan C, Ulrich, Zahlii, Yves Sucaet, sowberry, Norman "zEh" Fuchs, hitwork,
  68 * johnrembo, Brian Tafoya (http://www.premasolutions.com/), Nick Callen,
  69 * Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Philippe
  70 * Jausions (http://pear.php.net/user/jausions), Aidan Lister
  71 * (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
  72 * strcmp, Taras Bogach, jpfle, Alexander Ermolaev
  73 * (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
  74 * dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
  75 * (http://www.pedrotainha.com), James, penutbutterjelly, Arnout Kazemier
  76 * (http://www.3rd-Eden.com), 3D-GRAF, daniel airton wermann
  77 * (http://wermann.com.br), jakes, Yannoo, FGFEmperor, gabriel paderni, Atli
  78 * Þór, Maximusya, Diogo Resende, Rival, Howard Yeend, Allan Jensen
  79 * (http://www.winternet.no), davook, Benjamin Lupton, baris ozdil, Greg
  80 * Frazier, Manish, Matt Bradley, Cord, fearphage
  81 * (http://http/my.opera.com/fearphage/), Matteo, Victor, taith, Tim de
  82 * Koning, Ryan W Tenney (http://ryan.10e.us), Tod Gentille, Alexander M
  83 * Beedie, Riddler (http://www.frontierwebdev.com/), Luis Salazar
  84 * (http://www.freaky-media.com/), Rafał Kukawski, T.J. Leahy, Luke Smith
  85 * (http://lucassmith.name), Kheang Hok Chin (http://www.distantia.ca/),
  86 * Russell Walker (http://www.nbill.co.uk/), Jamie Beck
  87 * (http://www.terabit.ca/), Garagoth, Andrej Pavlovic, Dino, Le Torbi
  88 * (http://www.letorbi.de/), Ben (http://benblume.co.uk/), DtTvB
  89 * (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Michael, Chris
  90 * McMacken, setcookie, YUI Library:
  91 * http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Andreas,
  92 * Blues at http://hacks.bluesmoon.info/strftime/strftime.js, rem, Josep Sanz
  93 * (http://www.ws3.es/), Cagri Ekin, Lorenzo Pisani, incidence, Amirouche, Jay
  94 * Klehr, Amir Habibi (http://www.residence-mixte.com/), Tony, booeyOH, meo,
  95 * William, Greenseed, Yen-Wei Liu, Ben Bryan, Leslie Hoare, mk.keck
  96 * 
  97 * Dual licensed under the MIT (MIT-LICENSE.txt)
  98 * and GPL (GPL-LICENSE.txt) licenses.
  99 * 
 100 * Permission is hereby granted, free of charge, to any person obtaining a
 101 * copy of this software and associated documentation files (the
 102 * "Software"), to deal in the Software without restriction, including
 103 * without limitation the rights to use, copy, modify, merge, publish,
 104 * distribute, sublicense, and/or sell copies of the Software, and to
 105 * permit persons to whom the Software is furnished to do so, subject to
 106 * the following conditions:
 107 * 
 108 * The above copyright notice and this permission notice shall be included
 109 * in all copies or substantial portions of the Software.
 110 * 
 111 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 112 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 113 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 114 * IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
 115 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 116 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 117 * OTHER DEALINGS IN THE SOFTWARE.
 118 */ 
 119
 120
 121// jslint.com configuration options. See: http://wiki.github.com/kvz/phpjs/jslint-options
 122/* global window */
 123/* jslint adsafe: false, bitwise: false, browser: false, cap: false, css: false, debug: false, devel: false, eqeqeq: true, evil: false, forin: false, fragment: false, immed: true, indent: 4, laxbreak: false, maxerr: 100, maxlen: 80, newcap: true, nomen: false, on: true, onevar: false, passfail: false, plusplus: false, regexp: false, rhino: false, safe: false, sidebar: false, strict: false, sub: false, undef: true, white: false, widget: false */
 124
 125// Our idea with CommonJS is that you can do the following:
 126// var php = require('php');
 127// php.md5('test');
 128var php = {};
 129
 130php.ini_set = function (varname, newvalue) {
 131    // http://kevin.vanzonneveld.net
 132    // +   original by: Brett Zamir (http://brett-zamir.me)
 133    // %        note 1: This will not set a global_value or access level for the ini item
 134    // *     example 1: ini_set('date.timezone', 'America/Chicago');
 135    // *     returns 1: 'Asia/Hong_Kong'
 136
 137    var oldval = '',
 138        that = this;
 139    this.php_js = this.php_js || {};
 140    this.php_js.ini = this.php_js.ini || {};
 141    this.php_js.ini[varname] = this.php_js.ini[varname] || {};
 142    oldval = this.php_js.ini[varname].local_value;
 143
 144    var _setArr = function (oldval) { // Although these are set individually, they are all accumulated
 145        if (typeof oldval === 'undefined') {
 146            that.php_js.ini[varname].local_value = [];
 147        }
 148        that.php_js.ini[varname].local_value.push(newvalue);
 149    };
 150
 151    switch (varname) {
 152    case 'extension':
 153        if (typeof this.dl === 'function') {
 154            this.dl(newvalue); // This function is only experimental in php.js
 155        }
 156        _setArr(oldval, newvalue);
 157        break;
 158    default:
 159        this.php_js.ini[varname].local_value = newvalue;
 160        break;
 161    }
 162    return oldval;
 163};
 164php.ini_get = function (varname) {
 165    // http://kevin.vanzonneveld.net
 166    // +   original by: Brett Zamir (http://brett-zamir.me)
 167    // %        note 1: The ini values must be set by ini_set or manually within an ini file
 168    // *     example 1: ini_get('date.timezone');
 169    // *     returns 1: 'Asia/Hong_Kong'
 170    if (this.php_js && this.php_js.ini && this.php_js.ini[varname] && this.php_js.ini[varname].local_value !== undefined) {
 171        if (this.php_js.ini[varname].local_value === null) {
 172            return '';
 173        }
 174        return this.php_js.ini[varname].local_value;
 175    }
 176    return '';
 177}
 178
 179php.ctype_digit = function (text) {
 180    // http://kevin.vanzonneveld.net
 181    // +   original by: Brett Zamir (http://brett-zamir.me)
 182    // -    depends on: setlocale
 183    // *     example 1: ctype_digit('150');
 184    // *     returns 1: true
 185    if (typeof text !== 'string') {
 186        return false;
 187    }
 188    // BEGIN REDUNDANT
 189    this.setlocale('LC_ALL', 0); // ensure setup of localization variables takes place
 190    // END REDUNDANT
 191    return text.search(this.php_js.locales[this.php_js.localeCategories.LC_CTYPE].LC_CTYPE.dg) !== -1;
 192};
 193php.gmmktime = function () {
 194    // http://kevin.vanzonneveld.net
 195    // +   original by: Brett Zamir (http://brett-zamir.me)
 196    // +   derived from: mktime
 197    // *     example 1: gmmktime(14, 10, 2, 2, 1, 2008);
 198    // *     returns 1: 1201875002
 199    // *     example 2: gmmktime(0, 0, -1, 1, 1, 1970);
 200    // *     returns 2: -1
 201    var d = new Date(),
 202        r = arguments,
 203        i = 0,
 204        e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear'];
 205
 206    for (i = 0; i < e.length; i++) {
 207        if (typeof r[i] === 'undefined') {
 208            r[i] = d['getUTC' + e[i]]();
 209            r[i] += (i === 3); // +1 to fix JS months.
 210        } else {
 211            r[i] = parseInt(r[i], 10);
 212            if (isNaN(r[i])) {
 213                return false;
 214            }
 215        }
 216    }
 217
 218    // Map years 0-69 to 2000-2069 and years 70-100 to 1970-2000.
 219    r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0);
 220
 221    // Set year, month (-1 to fix JS months), and date.
 222    // !This must come before the call to setHours!
 223    d.setUTCFullYear(r[5], r[3] - 1, r[4]);
 224
 225    // Set hours, minutes, and seconds.
 226    d.setUTCHours(r[0], r[1], r[2]);
 227
 228    // Divide milliseconds by 1000 to return seconds and drop decimal.
 229    // Add 1 second if negative or it'll be off from PHP by 1 second.
 230    return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
 231};
 232php.getdate = function (timestamp) {
 233    // http://kevin.vanzonneveld.net
 234    // +   original by: Paulo Freitas
 235    // +   input by: Alex
 236    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
 237    // *     example 1: getdate(1055901520);
 238    // *     returns 1: {'seconds': 40, 'minutes': 58, 'hours': 21, 'mday': 17, 'wday': 2, 'mon': 6, 'year': 2003, 'yday': 167, 'weekday': 'Tuesday', 'month': 'June', '0': 1055901520}
 239    var _w = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
 240    var _m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
 241    var d = ((typeof(timestamp) == 'undefined') ? new Date() : // Not provided
 242    (typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date()
 243    new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
 244    );
 245    var w = d.getDay();
 246    var m = d.getMonth();
 247    var y = d.getFullYear();
 248    var r = {};
 249
 250    r.seconds = d.getSeconds();
 251    r.minutes = d.getMinutes();
 252    r.hours = d.getHours();
 253    r.mday = d.getDate();
 254    r.wday = w;
 255    r.mon = m + 1;
 256    r.year = y;
 257    r.yday = Math.floor((d - (new Date(y, 0, 1))) / 86400000);
 258    r.weekday = _w[w] + 'day';
 259    r.month = _m[m];
 260    r['0'] = parseInt(d.getTime() / 1000, 10);
 261
 262    return r;
 263};
 264php.checkdate = function (m, d, y) {
 265    // Returns true(1) if it is a valid date in gregorian calendar  
 266    // 
 267    // version: 1103.1210
 268    // discuss at: http://phpjs.org/functions/checkdate
 269    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 270    // +   improved by: Pyerre
 271    // +   improved by: Theriault
 272    // *     example 1: checkdate(12, 31, 2000);
 273    // *     returns 1: true
 274    // *     example 2: checkdate(2, 29, 2001);
 275    // *     returns 2: false
 276    // *     example 3: checkdate(3, 31, 2008);
 277    // *     returns 3: true
 278    // *     example 4: checkdate(1, 390, 2000);
 279    // *     returns 4: false
 280    return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
 281};
 282php.gmdate = function (format, timestamp) {
 283    // Format a GMT date/time  
 284    // 
 285    // version: 1103.1210
 286    // discuss at: http://phpjs.org/functions/gmdate
 287    // +   original by: Brett Zamir (http://brett-zamir.me)
 288    // +   input by: Alex
 289    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
 290    // -    depends on: date
 291    // *     example 1: gmdate('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400); // Return will depend on your timezone
 292    // *     returns 1: '07:09:40 m is month'
 293    var dt = ((typeof(timestamp) == 'undefined') ? new Date() : // Not provided
 294    (typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date()
 295    new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
 296    );
 297    timestamp = Date.parse(dt.toUTCString().slice(0, -4)) / 1000;
 298    return this.date(format, timestamp);
 299};
 300php.mt_rand = function (min, max) {
 301    // Returns a random number from Mersenne Twister  
 302    // 
 303    // version: 1103.1210
 304    // discuss at: http://phpjs.org/functions/mt_rand
 305    // +   original by: Onno Marsman
 306    // *     example 1: mt_rand(1, 1);
 307    // *     returns 1: 1
 308    var argc = arguments.length;
 309    if (argc === 0) {
 310        min = 0;
 311        max = 2147483647;
 312    } else if (argc === 1) {
 313        throw new Error('Warning: mt_rand() expects exactly 2 parameters, 1 given');
 314    }
 315    return Math.floor(Math.random() * (max - min + 1)) + min;
 316};
 317
 318php.abs = function (mixed_number) {
 319    // Return the absolute value of the number  
 320    // 
 321    // version: 1103.1210
 322    // discuss at: http://phpjs.org/functions/abs
 323    // +   original by: Waldo Malqui Silva
 324    // +   improved by: Karol Kowalski
 325    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 326    // +   improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
 327    // *     example 1: \php.abs(4.2);
 328    // *     returns 1: 4.2
 329    // *     example 2: \php.abs(-4.2);
 330    // *     returns 2: 4.2
 331    // *     example 3: \php.abs(-5);
 332    // *     returns 3: 5
 333    // *     example 4: \php.abs('_argos');
 334    // *     returns 4: 0
 335    return Math.abs(mixed_number) || 0;
 336};
 337
 338php.addcslashes = function (str, charlist) {
 339    // Escapes all chars mentioned in charlist with backslash. It creates octal representations if asked to backslash characters with 8th bit set or with ASCII<32 (except '\n', '\r', '\t' etc...)  
 340    // 
 341    // version: 1103.1210
 342    // discuss at: http://phpjs.org/functions/addcslashes
 343    // +   original by: Brett Zamir (http://brett-zamir.me)
 344    // %  note 1: We show double backslashes in the return value example code below because a JavaScript string will not
 345    // %  note 1: render them as backslashes otherwise
 346    // *     example 1: \php.addcslashes('foo[ ]', 'A..z'); // Escape all ASCII within capital A to lower z range, including square brackets
 347    // *     returns 1: "\\f\\o\\o\\[ \\]"
 348    // *     example 2: \php.addcslashes("zoo['.']", 'z..A'); // Only escape z, period, and A here since not a lower-to-higher range
 349    // *     returns 2: "\\zoo['\\.']"
 350    // *     example 3: \php.addcslashes("@a\u0000\u0010\u00A9", "\0..\37!@\177..\377") == '\\@a\\000\\020\\302\\251'); // Escape as octals those specified and less than 32 (0x20) or greater than 126 (0x7E), but not otherwise
 351    // *     returns 3: true
 352    // *     example 4: \php.addcslashes("\u0020\u007E", "\40..\175") == '\\ ~'); // Those between 32 (0x20 or 040) and 126 (0x7E or 0176) decimal value will be backslashed if specified (not octalized)
 353    // *     returns 4: true
 354    // *     example 5: \php.addcslashes("\r\u0007\n", '\0..\37'); // Recognize C escape sequences if specified
 355    // *     returns 5: "\\r\\a\\n"
 356    // *     example 6: \php.addcslashes("\r\u0007\n", '\0'); // Do not recognize C escape sequences if not specified
 357    // *     returns 7: "\r\u0007\n"
 358    var target = '',
 359        chrs = [],
 360        i = 0,
 361        j = 0,
 362        c = '',
 363        next = '',
 364        rangeBegin = '',
 365        rangeEnd = '',
 366        chr = '',
 367        begin = 0,
 368        end = 0,
 369        octalLength = 0,
 370        postOctalPos = 0,
 371        cca = 0,
 372        escHexGrp = [],
 373        encoded = '',
 374        percentHex = /%([\dA-Fa-f]+)/g;
 375    var _pad = function (n, c) {
 376        if ((n = n + "").length < c) {
 377            return new Array(++c - n.length).join("0") + n;
 378        } else {
 379            return n;
 380        }
 381    };
 382
 383    for (i = 0; i < charlist.length; i++) {
 384        c = charlist.charAt(i);
 385        next = charlist.charAt(i + 1);
 386        if (c === '\\' && next && (/\d/).test(next)) { // Octal
 387            rangeBegin = charlist.slice(i + 1).match(/^\d+/)[0];
 388            octalLength = rangeBegin.length;
 389            postOctalPos = i + octalLength + 1;
 390            if (charlist.charAt(postOctalPos) + charlist.charAt(postOctalPos + 1) === '..') { // Octal begins range
 391                begin = rangeBegin.charCodeAt(0);
 392                if ((/\\\d/).test(charlist.charAt(postOctalPos + 2) + charlist.charAt(postOctalPos + 3))) { // Range ends with octal
 393                    rangeEnd = charlist.slice(postOctalPos + 3).match(/^\d+/)[0];
 394                    i += 1; // Skip range end backslash
 395                } else if (charlist.charAt(postOctalPos + 2)) { // Range ends with character
 396                    rangeEnd = charlist.charAt(postOctalPos + 2);
 397                } else {
 398                    throw 'Range with no end point';
 399                }
 400                end = rangeEnd.charCodeAt(0);
 401                if (end > begin) { // Treat as a range
 402                    for (j = begin; j <= end; j++) {
 403                        chrs.push(String.fromCharCode(j));
 404                    }
 405                } else { // Supposed to treat period, begin and end as individual characters only, not a range
 406                    chrs.push('.', rangeBegin, rangeEnd);
 407                }
 408                i += rangeEnd.length + 2; // Skip dots and range end (already skipped range end backslash if present)
 409            } else { // Octal is by itself
 410                chr = String.fromCharCode(parseInt(rangeBegin, 8));
 411                chrs.push(chr);
 412            }
 413            i += octalLength; // Skip range begin
 414        } else if (next + charlist.charAt(i + 2) === '..') { // Character begins range
 415            rangeBegin = c;
 416            begin = rangeBegin.charCodeAt(0);
 417            if ((/\\\d/).test(charlist.charAt(i + 3) + charlist.charAt(i + 4))) { // Range ends with octal
 418                rangeEnd = charlist.slice(i + 4).match(/^\d+/)[0];
 419                i += 1; // Skip range end backslash
 420            } else if (charlist.charAt(i + 3)) { // Range ends with character
 421                rangeEnd = charlist.charAt(i + 3);
 422            } else {
 423                throw 'Range with no end point';
 424            }
 425            end = rangeEnd.charCodeAt(0);
 426            if (end > begin) { // Treat as a range
 427                for (j = begin; j <= end; j++) {
 428                    chrs.push(String.fromCharCode(j));
 429                }
 430            } else { // Supposed to treat period, begin and end as individual characters only, not a range
 431                chrs.push('.', rangeBegin, rangeEnd);
 432            }
 433            i += rangeEnd.length + 2; // Skip dots and range end (already skipped range end backslash if present)
 434        } else { // Character is by itself
 435            chrs.push(c);
 436        }
 437    }
 438
 439    for (i = 0; i < str.length; i++) {
 440        c = str.charAt(i);
 441        if (chrs.indexOf(c) !== -1) {
 442            target += '\\';
 443            cca = c.charCodeAt(0);
 444            if (cca < 32 || cca > 126) { // Needs special escaping
 445                switch (c) {
 446                case '\n':
 447                    target += 'n';
 448                    break;
 449                case '\t':
 450                    target += 't';
 451                    break;
 452                case '\u000D':
 453                    target += 'r';
 454                    break;
 455                case '\u0007':
 456                    target += 'a';
 457                    break;
 458                case '\v':
 459                    target += 'v';
 460                    break;
 461                case '\b':
 462                    target += 'b';
 463                    break;
 464                case '\f':
 465                    target += 'f';
 466                    break;
 467                default:
 468                    //target += _pad(cca.toString(8), 3);break; // Sufficient for UTF-16
 469                    encoded = encodeURIComponent(c);
 470
 471                    // 3-length-padded UTF-8 octets
 472                    if ((escHexGrp = percentHex.exec(encoded)) !== null) {
 473                        target += _pad(parseInt(escHexGrp[1], 16).toString(8), 3); // already added a slash above
 474                    }
 475                    while ((escHexGrp = percentHex.exec(encoded)) !== null) {
 476                        target += '\\' + _pad(parseInt(escHexGrp[1], 16).toString(8), 3);
 477                    }
 478                    break;
 479                }
 480            } else { // Perform regular backslashed escaping
 481                target += c;
 482            }
 483        } else { // Just add the character unescaped
 484            target += c;
 485        }
 486    }
 487    return target;
 488};
 489
 490php.addslashes = function (str) {
 491    // Escapes single quote, double quotes and backslash characters in a string with backslashes  
 492    // 
 493    // version: 1103.1210
 494    // discuss at: http://phpjs.org/functions/addslashes
 495    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 496    // +   improved by: Ates Goral (http://magnetiq.com)
 497    // +   improved by: marrtins
 498    // +   improved by: Nate
 499    // +   improved by: Onno Marsman
 500    // +   input by: Denny Wardhana
 501    // +   improved by: Brett Zamir (http://brett-zamir.me)
 502    // +   improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
 503    // *     example 1: \php.addslashes("kevin's birthday");
 504    // *     returns 1: 'kevin\'s birthday'
 505    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
 506};
 507
 508php.array_chunk = function (input, size) {
 509    // Split array into chunks  
 510    // 
 511    // version: 1103.1210
 512    // discuss at: http://phpjs.org/functions/array_chunk
 513    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
 514    // *     example 1: \php.array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
 515    // *     returns 1: {0 : {0: 'Kevin', 1: 'van'} , 1 : {0: 'Zonneveld'}}
 516    for (var x, i = 0, c = -1, l = input.length, n = []; i < l; i++) {
 517        (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
 518    }
 519
 520    return n;
 521};
 522
 523php.array_combine = function (keys, values) {
 524    // Creates an array by using the elements of the first parameter as keys and the elements of the second as the corresponding values  
 525    // 
 526    // version: 1103.1210
 527    // discuss at: http://phpjs.org/functions/array_combine
 528    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 529    // +   improved by: Brett Zamir (http://brett-zamir.me)
 530    // *     example 1: \php.array_combine([0,1,2], ['kevin','van','zonneveld']);
 531    // *     returns 1: {0: 'kevin', 1: 'van', 2: 'zonneveld'}
 532    var new_array = {},
 533        keycount = keys && keys.length,
 534        i = 0;
 535
 536    // input sanitation
 537    if (typeof keys !== 'object' || typeof values !== 'object' || // Only accept arrays or array-like objects
 538    typeof keycount !== 'number' || typeof values.length !== 'number' || !keycount) { // Require arrays to have a count
 539        return false;
 540    }
 541
 542    // number of elements does not match
 543    if (keycount != values.length) {
 544        return false;
 545    }
 546
 547    for (i = 0; i < keycount; i++) {
 548        new_array[keys[i]] = values[i];
 549    }
 550
 551    return new_array;
 552};
 553
 554php.array_diff = function () {
 555    // Returns the entries of arr1 that have values which are not present in any of the others arguments.  
 556    // 
 557    // version: 1103.1210
 558    // discuss at: http://phpjs.org/functions/array_diff
 559    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 560    // +   improved by: Sanjoy Roy
 561    // +    revised by: Brett Zamir (http://brett-zamir.me)
 562    // *     example 1: \php.array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld']);
 563    // *     returns 1: {0:'Kevin'}
 564    var arr1 = arguments[0],
 565        retArr = {};
 566    var k1 = '',
 567        i = 1,
 568        k = '',
 569        arr = {};
 570
 571    arr1keys: for (k1 in arr1) {
 572        for (i = 1; i < arguments.length; i++) {
 573            arr = arguments[i];
 574            for (k in arr) {
 575                if (arr[k] === arr1[k1]) {
 576                    // If it reaches here, it was found in at least one array, so try next value
 577                    continue arr1keys;
 578                }
 579            }
 580            retArr[k1] = arr1[k1];
 581        }
 582    }
 583
 584    return retArr;
 585};
 586
 587php.array_fill = function (start_index, num, mixed_val) {
 588    // Create an array containing num elements starting with index start_key each initialized to val  
 589    // 
 590    // version: 1103.1210
 591    // discuss at: http://phpjs.org/functions/array_fill
 592    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 593    // +   improved by: Waldo Malqui Silva
 594    // *     example 1: \php.array_fill(5, 6, 'banana');
 595    // *     returns 1: { 5: 'banana', 6: 'banana', 7: 'banana', 8: 'banana', 9: 'banana', 10: 'banana' }
 596    var key, tmp_arr = {};
 597
 598    if (!isNaN(start_index) && !isNaN(num)) {
 599        for (key = 0; key < num; key++) {
 600            tmp_arr[(key + start_index)] = mixed_val;
 601        }
 602    }
 603
 604    return tmp_arr;
 605};
 606
 607php.array_fill_keys = function (keys, value) {
 608    // Create an array using the elements of the first parameter as keys each initialized to val  
 609    // 
 610    // version: 1103.1210
 611    // discuss at: http://phpjs.org/functions/array_fill_keys
 612    // +   original by: Brett Zamir (http://brett-zamir.me)
 613    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
 614    // *     example 1: \php.keys = {'a': 'foo', 2: 5, 3: 10, 4: 'bar'}
 615    // *     example 1: \php.array_fill_keys(keys, 'banana')
 616    // *     returns 1: {"foo": "banana", 5: "banana", 10: "banana", "bar": "banana"}
 617    var retObj = {},
 618        key = '';
 619
 620    for (key in keys) {
 621        retObj[keys[key]] = value;
 622    }
 623
 624    return retObj;
 625};
 626
 627php.array_filter = function (arr, func) {
 628    // Filters elements from the array via the callback.  
 629    // 
 630    // version: 1103.1210
 631    // discuss at: http://phpjs.org/functions/array_filter
 632    // +   original by: Brett Zamir (http://brett-zamir.me)
 633    // %        note 1: Takes a function as an argument, not a function's name
 634    // *     example 1: \php.var odd = function (num) {return (num & 1);}; 
 635    // *     example 1: \php.array_filter({"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}, odd);
 636    // *     returns 1: {"a": 1, "c": 3, "e": 5}
 637    // *     example 2: \php.var even = function (num) {return (!(num & 1));}
 638    // *     example 2: \php.array_filter([6, 7, 8, 9, 10, 11, 12], even);
 639    // *     returns 2: {0: 6, 2: 8, 4: 10, 6: 12} 
 640    var retObj = {},
 641        k;
 642
 643    for (k in arr) {
 644        if (func(arr[k])) {
 645            retObj[k] = arr[k];
 646        }
 647    }
 648
 649    return retObj;
 650};
 651
 652php.array_flip = function (trans) {
 653    // Return array with key <-> value flipped  
 654    // 
 655    // version: 1103.1210
 656    // discuss at: http://phpjs.org/functions/array_flip
 657    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 658    // *     example 1: \php.array_flip( {a: 1, b: 1, c: 2} );
 659    // *     returns 1: {1: 'b', 2: 'c'}
 660    var key, tmp_ar = {};
 661
 662    for (key in trans) {
 663        tmp_ar[trans[key]] = key;
 664    }
 665
 666    return tmp_ar;
 667};
 668
 669php.array_intersect = function () {
 670    // Returns the entries of arr1 that have values which are present in all the other arguments  
 671    // 
 672    // version: 1103.1210
 673    // discuss at: http://phpjs.org/functions/array_intersect
 674    // +   original by: Brett Zamir (http://brett-zamir.me)
 675    // %        note 1: These only output associative arrays (would need to be
 676    // %        note 1: all numeric and counting from zero to be numeric)
 677    // *     example 1: $array1 = {'a' : 'green', 0:'red', 1: 'blue'};
 678    // *     example 1: $array2 = {'b' : 'green', 0:'yellow', 1:'red'};
 679    // *     example 1: $array3 = ['green', 'red'];
 680    // *     example 1: $result = array_intersect($array1, $array2, $array3);
 681    // *     returns 1: {0: 'red', a: 'green'}
 682    var arr1 = arguments[0],
 683        retArr = {};
 684    var k1 = '',
 685        arr = {},
 686        i = 0,
 687        k = '';
 688
 689    arr1keys: for (k1 in arr1) {
 690        arrs: for (i = 1; i < arguments.length; i++) {
 691            arr = arguments[i];
 692            for (k in arr) {
 693                if (arr[k] === arr1[k1]) {
 694                    if (i === arguments.length - 1) {
 695                        retArr[k1] = arr1[k1];
 696                    }
 697                    // If the innermost loop always leads at least once to an equal value, continue the loop until done
 698                    continue arrs;
 699                }
 700            }
 701            // If it reaches here, it wasn't found in at least one array, so try next value
 702            continue arr1keys;
 703        }
 704    }
 705
 706    return retArr;
 707};
 708
 709php.array_key_exists = function (key, search) {
 710    // Checks if the given key or index exists in the array  
 711    // 
 712    // version: 1103.1210
 713    // discuss at: http://phpjs.org/functions/array_key_exists
 714    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 715    // +   improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
 716    // *     example 1: \php.array_key_exists('kevin', {'kevin': 'van Zonneveld'});
 717    // *     returns 1: true
 718    // input sanitation
 719    if (!search || (search.constructor !== Array && search.constructor !== Object)) {
 720        return false;
 721    }
 722
 723    return key in search;
 724};
 725
 726php.array_keys = function (input, search_value, argStrict) {
 727    // Return just the keys from the input array, optionally only for the specified search_value  
 728    // 
 729    // version: 1103.1210
 730    // discuss at: http://phpjs.org/functions/array_keys
 731    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 732    // +      input by: Brett Zamir (http://brett-zamir.me)
 733    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 734    // +   improved by: jd
 735    // +   improved by: Brett Zamir (http://brett-zamir.me)
 736    // *     example 1: \php.array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );
 737    // *     returns 1: {0: 'firstname', 1: 'surname'}
 738    var search = typeof search_value !== 'undefined',
 739        tmp_arr = [],
 740        strict = !!argStrict,
 741        include = true,
 742        key = '';
 743
 744    for (key in input) {
 745        if (input.hasOwnProperty(key)) {
 746            include = true;
 747            if (search) {
 748                if (strict && input[key] !== search_value) {
 749                    include = false;
 750                }
 751                else if (input[key] != search_value) {
 752                    include = false;
 753                }
 754            }
 755
 756            if (include) {
 757                tmp_arr[tmp_arr.length] = key;
 758            }
 759        }
 760    }
 761
 762    return tmp_arr;
 763};
 764
 765php.array_map = function (callback) {
 766    // Applies the callback to the elements in given arrays.  
 767    // 
 768    // version: 1103.1210
 769    // discuss at: http://phpjs.org/functions/array_map
 770    // +   original by: Andrea Giammarchi (http://webreflection.blogspot.com)
 771    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 772    // +   improved by: Brett Zamir (http://brett-zamir.me)
 773    // %        note 1: Takes a function as an argument, not a function's name
 774    // %        note 2: If the callback is a string, it can only work if the function name is in the global context
 775    // *     example 1: \php.array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] );
 776    // *     returns 1: [ 1, 8, 27, 64, 125 ]
 777    var argc = arguments.length,
 778        argv = arguments;
 779    var j = argv[1].length,
 780        i = 0,
 781        k = 1,
 782        m = 0;
 783    var tmp = [],
 784        tmp_ar = [];
 785
 786    while (i < j) {
 787        while (k < argc) {
 788            tmp[m++] = argv[k++][i];
 789        }
 790
 791        m = 0;
 792        k = 1;
 793
 794        if (callback) {
 795            if (typeof callback === 'string') {
 796                callback = this.window[callback];
 797            }
 798            tmp_ar[i++] = callback.apply(null, tmp);
 799        } else {
 800            tmp_ar[i++] = tmp;
 801        }
 802
 803        tmp = [];
 804    }
 805
 806    return tmp_ar;
 807};
 808
 809php.array_merge = function () {
 810    // Merges elements from passed arrays into one array  
 811    // 
 812    // version: 1103.1210
 813    // discuss at: http://phpjs.org/functions/array_merge
 814    // +   original by: Brett Zamir (http://brett-zamir.me)
 815    // +   bugfixed by: Nate
 816    // +   input by: josh
 817    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
 818    // *     example 1: \php.arr1 = {"color": "red", 0: 2, 1: 4}
 819    // *     example 1: \php.arr2 = {0: "a", 1: "b", "color": "green", "shape": "trapezoid", 2: 4}
 820    // *     example 1: \php.array_merge(arr1, arr2)
 821    // *     returns 1: {"color": "green", 0: 2, 1: 4, 2: "a", 3: "b", "shape": "trapezoid", 4: 4}
 822    // *     example 2: \php.arr1 = []
 823    // *     example 2: \php.arr2 = {1: "data"}
 824    // *     example 2: \php.array_merge(arr1, arr2)
 825    // *     returns 2: {0: "data"}
 826    var args = Array.prototype.slice.call(arguments),
 827        retObj = {},
 828        k, j = 0,
 829        i = 0,
 830        retArr = true;
 831
 832    for (i = 0; i < args.length; i++) {
 833        if (!(args[i] instanceof Array)) {
 834            retArr = false;
 835            break;
 836        }
 837    }
 838
 839    if (retArr) {
 840        retArr = [];
 841        for (i = 0; i < args.length; i++) {
 842            retArr = retArr.concat(args[i]);
 843        }
 844        return retArr;
 845    }
 846    var ct = 0;
 847
 848    for (i = 0, ct = 0; i < args.length; i++) {
 849        if (args[i] instanceof Array) {
 850            for (j = 0; j < args[i].length; j++) {
 851                retObj[ct++] = args[i][j];
 852            }
 853        } else {
 854            for (k in args[i]) {
 855                if (args[i].hasOwnProperty(k)) {
 856                    if (parseInt(k, 10) + '' === k) {
 857                        retObj[ct++] = args[i][k];
 858                    } else {
 859                        retObj[k] = args[i][k];
 860                    }
 861                }
 862            }
 863        }
 864    }
 865    return retObj;
 866};
 867
 868php.array_merge_recursive = function (arr1, arr2) {
 869    // Recursively merges elements from passed arrays into one array  
 870    // 
 871    // version: 1103.1210
 872    // discuss at: http://phpjs.org/functions/array_merge_recursive
 873    // +   original by: Subhasis Deb
 874    // +      input by: Brett Zamir (http://brett-zamir.me)
 875    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 876    // -    depends on: array_merge
 877    // *     example 1: \php.arr1 = {'color': {'favourite': 'read'}, 0: 5}
 878    // *     example 1: \php.arr2 = {0: 10, 'color': {'favorite': 'green', 0: 'blue'}}
 879    // *     example 1: \php.array_merge_recursive(arr1, arr2)
 880    // *     returns 1: {'color': {'favorite': {0: 'red', 1: 'green'}, 0: 'blue'}, 1: 5, 1: 10}
 881    var idx = '';
 882
 883    if ((arr1 && (arr1 instanceof Array)) && (arr2 && (arr2 instanceof Array))) {
 884        for (idx in arr2) {
 885            arr1.push(arr2[idx]);
 886        }
 887    } else if ((arr1 && (arr1 instanceof Object)) && (arr2 && (arr2 instanceof Object))) {
 888        for (idx in arr2) {
 889            if (idx in arr1) {
 890                if (typeof arr1[idx] == 'object' && typeof arr2 == 'object') {
 891                    arr1[idx] = this.array_merge(arr1[idx], arr2[idx]);
 892                } else {
 893                    arr1[idx] = arr2[idx];
 894                }
 895            } else {
 896                arr1[idx] = arr2[idx];
 897            }
 898        }
 899    }
 900
 901    return arr1;
 902};
 903
 904php.array_pop = function (inputArr) {
 905    // Pops an element off the end of the array  
 906    // 
 907    // version: 1103.1210
 908    // discuss at: http://phpjs.org/functions/array_pop
 909    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 910    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 911    // +      input by: Brett Zamir (http://brett-zamir.me)
 912    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 913    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
 914    // +   input by: Theriault
 915    // %        note 1: While IE (and other browsers) support iterating an object's
 916    // %        note 1: own properties in order, if one attempts to add back properties
 917    // %        note 1: in IE, they may end up in their former position due to their position
 918    // %        note 1: being retained. So use of this function with "associative arrays"
 919    // %        note 1: (objects) may lead to unexpected behavior in an IE environment if
 920    // %        note 1: you add back properties with the same keys that you removed
 921    // *     example 1: \php.array_pop([0,1,2]);
 922    // *     returns 1: 2
 923    // *     example 2: \php.data = {firstName: 'Kevin', surName: 'van Zonneveld'};
 924    // *     example 2: \php.lastElem = array_pop(data);
 925    // *     returns 2: 'van Zonneveld'
 926    // *     results 2: data == {firstName: 'Kevin'}
 927    var key = '',
 928        lastKey = '';
 929
 930    if (inputArr.hasOwnProperty('length')) {
 931        // Indexed
 932        if (!inputArr.length) {
 933            // Done popping, are we?
 934            return null;
 935        }
 936        return inputArr.pop();
 937    } else {
 938        // Associative
 939        for (key in inputArr) {
 940            if (inputArr.hasOwnProperty(key)) {
 941                lastKey = key;
 942            }
 943        }
 944        if (lastKey) {
 945            var tmp = inputArr[lastKey];
 946            delete(inputArr[lastKey]);
 947            return tmp;
 948        } else {
 949            return null;
 950        }
 951    }
 952};
 953
 954php.array_push = function (inputArr) {
 955    // Pushes elements onto the end of the array  
 956    // 
 957    // version: 1103.1210
 958    // discuss at: http://phpjs.org/functions/array_push
 959    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 960    // +   improved by: Brett Zamir (http://brett-zamir.me)
 961    // %        note 1: Note also that IE retains information about property position even
 962    // %        note 1: after being supposedly deleted, so if you delete properties and then
 963    // %        note 1: add back properties with the same keys (including numeric) that had
 964    // %        note 1: been deleted, the order will be as before; thus, this function is not
 965    // %        note 1: really recommended with associative arrays (objects) in IE environments
 966    // *     example 1: \php.array_push(['kevin','van'], 'zonneveld');
 967    // *     returns 1: 3
 968    var i = 0,
 969        pr = '',
 970        argv = arguments,
 971        argc = argv.length,
 972        allDigits = /^\d$/,
 973        size = 0,
 974        highestIdx = 0,
 975        len = 0;
 976    if (inputArr.hasOwnProperty('length')) {
 977        for (i = 1; i < argc; i++) {
 978            inputArr[inputArr.length] = argv[i];
 979        }
 980        return inputArr.length;
 981    }
 982
 983    // Associative (object)
 984    for (pr in inputArr) {
 985        if (inputArr.hasOwnProperty(pr)) {
 986            ++len;
 987            if (pr.search(allDigits) !== -1) {
 988                size = parseInt(pr, 10);
 989                highestIdx = size > highestIdx ? size : highestIdx;
 990            }
 991        }
 992    }
 993    for (i = 1; i < argc; i++) {
 994        inputArr[++highestIdx] = argv[i];
 995    }
 996    return len + i - 1;
 997};
 998
 999php.array_reduce = function (a_input, callback) {
1000    // Iteratively reduce the array to a single value via the callback.  
1001    // 
1002    // version: 1103.1210
1003    // discuss at: http://phpjs.org/functions/array_reduce
1004    // +   original by: Alfonso Jimenez (http://www.alfonsojimenez.com)
1005    // %        note 1: Takes a function as an argument, not a function's name
1006    // *     example 1: \php.array_reduce([1, 2, 3, 4, 5], function (v, w){v += w;return v;});
1007    // *     returns 1: 15
1008    var lon = a_input.length;
1009    var res = 0,
1010        i = 0;
1011    var tmp = [];
1012
1013
1014    for (i = 0; i < lon; i += 2) {
1015        tmp[0] = a_input[i];
1016        if (a_input[(i + 1)]) {
1017            tmp[1] = a_input[(i + 1)];
1018        } else {
1019            tmp[1] = 0;
1020        }
1021        res += callback.apply(null, tmp);
1022        tmp = [];
1023    }
1024
1025    return res;
1026};
1027
1028php.array_reverse = function (array, preserve_keys) {
1029    // Return input as a new array with the order of the entries reversed  
1030    // 
1031    // version: 1103.1210
1032    // discuss at: http://phpjs.org/functions/array_reverse
1033    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1034    // +   improved by: Karol Kowalski
1035    // *     example 1: \php.array_reverse( [ 'php', '4.0', ['green', 'red'] ], true);
1036    // *     returns 1: { 2: ['green', 'red'], 1: 4, 0: 'php'}
1037    var arr_len = array.length,
1038        newkey = 0,
1039        tmp_arr = {},
1040        key = '';
1041    preserve_keys = !! preserve_keys;
1042
1043    for (key in array) {
1044        newkey = arr_len - key - 1;
1045        tmp_arr[preserve_keys ? key : newkey] = array[key];
1046    }
1047
1048    return tmp_arr;
1049};
1050
1051php.array_shift = function (inputArr) {
1052    // Pops an element off the beginning of the array  
1053    // 
1054    // version: 1103.1210
1055    // discuss at: http://phpjs.org/functions/array_shift
1056    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1057    // +   improved by: Martijn Wieringa
1058    // %        note 1: Currently does not handle objects
1059    // *     example 1: \php.array_shift(['Kevin', 'van', 'Zonneveld']);
1060    // *     returns 1: 'Kevin'
1061    var props = false,
1062        shift = undefined,
1063        pr = '',
1064        allDigits = /^\d$/,
1065        int_ct = -1,
1066        _checkToUpIndices = function (arr, ct, key) {
1067            // Deal with situation, e.g., if encounter index 4 and try to set it to 0, but 0 exists later in loop (need to
1068            // increment all subsequent (skipping current key, since we need its value below) until find unused)
1069            if (arr[ct] !== undefined) {
1070                var tmp = ct;
1071                ct += 1;
1072                if (ct === key) {
1073                    ct += 1;
1074                }
1075                ct = _checkToUpIndices(arr, ct, key);
1076                arr[ct] = arr[tmp];
1077                delete arr[tmp];
1078            }
1079            return ct;
1080        };
1081
1082
1083    if (inputArr.length === 0) {
1084        return null;
1085    }
1086    if (inputArr.length > 0) {
1087        return inputArr.shift();
1088    }
1089
1090/*
1091    UNFINISHED FOR HANDLING OBJECTS
1092    for (pr in inputArr) {
1093        if (inputArr.hasOwnProperty(pr)) {
1094            props = true;
1095            shift = inputArr[pr];
1096            delete inputArr[pr];
1097            break;
1098        }
1099    }
1100    for (pr in inputArr) {
1101        if (inputArr.hasOwnProperty(pr)) {
1102            if (pr.search(allDigits) !== -1) {
1103                int_ct += 1;
1104                if (parseInt(pr, 10) === int_ct) { // Key is already numbered ok, so don't need to change key for value
1105                    continue;
1106                }
1107                _checkToUpIndices(inputArr, int_ct, pr);
1108                arr[int_ct] = arr[pr];
1109                delete arr[pr];
1110            }
1111        }
1112    }
1113    if (!props) {
1114        return null;
1115    }
1116    return shift;
1117    */
1118};
1119
1120php.array_slice = function (arr, offst, lgth, preserve_keys) {
1121    // Returns elements specified by offset and length  
1122    // 
1123    // version: 1103.1210
1124    // discuss at: http://phpjs.org/functions/array_slice
1125    // +   original by: Brett Zamir (http://brett-zamir.me)
1126    // -    depends on: is_int
1127    // +      input by: Brett Zamir (http://brett-zamir.me)
1128    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1129    // %          note: Relies on is_int because !isNaN accepts floats 
1130    // *     example 1: \php.array_slice(["a", "b", "c", "d", "e"], 2, -1);
1131    // *     returns 1: {0: 'c', 1: 'd'}
1132    // *     example 2: \php.array_slice(["a", "b", "c", "d", "e"], 2, -1, true);
1133    // *     returns 2: {2: 'c', 3: 'd'}
1134/*
1135    if ('callee' in arr && 'length' in arr) {
1136        arr = Array.prototype.slice.call(arr);
1137    }
1138    */
1139
1140    var key = '';
1141
1142    if (!(arr instanceof Array) || (preserve_keys && offst !== 0)) { // Assoc. array as input or if required as output
1143        var lgt = 0,
1144            newAssoc = {};
1145        for (key in arr) {
1146            //if (key !== 'length') {
1147            lgt += 1;
1148            newAssoc[key] = arr[key];
1149            //}
1150        }
1151        arr = newAssoc;
1152
1153        offst = (offst < 0) ? lgt + offst : offst;
1154        lgth = lgth === undefined ? lgt : (lgth < 0) ? lgt + lgth - offst : lgth;
1155
1156        var assoc = {};
1157        var start = false,
1158            it = -1,
1159            arrlgth = 0,
1160            no_pk_idx = 0;
1161        for (key in arr) {
1162            ++it;
1163            if (arrlgth >= lgth) {
1164                break;
1165            }
1166            if (it == offst) {
1167                start = true;
1168            }
1169            if (!start) {
1170                continue;
1171            }++arrlgth;
1172            if (this.is_int(key) && !preserve_keys) {
1173                assoc[no_pk_idx++] = arr[key];
1174            } else {
1175                assoc[key] = arr[key];
1176            }
1177        }
1178        //assoc.length = arrlgth; // Make as array-like object (though length will not be dynamic)
1179        return assoc;
1180    }
1181
1182    if (lgth === undefined) {
1183        return arr.slice(offst);
1184    } else if (lgth >= 0) {
1185        return arr.slice(offst, offst + lgth);
1186    } else {
1187        return arr.slice(offst, lgth);
1188    }
1189};
1190
1191php.array_splice = function (arr, offst, lgth, replacement) {
1192    // Removes the elements designated by offset and length and replace them with supplied array  
1193    // 
1194    // version: 1103.1210
1195    // discuss at: http://phpjs.org/functions/array_splice
1196    // +   original by: Brett Zamir (http://brett-zamir.me)
1197    // +   input by: Theriault
1198    // %        note 1: Order does get shifted in associative array input with numeric indices,
1199    // %        note 1: since PHP behavior doesn't preserve keys, but I understand order is
1200    // %        note 1: not reliable anyways
1201    // %        note 2: Note also that IE retains information about property position even
1202    // %        note 2: after being supposedly deleted, so use of this function may produce
1203    // %        note 2: unexpected results in IE if you later attempt to add back properties
1204    // %        note 2: with the same keys that had been deleted
1205    // -    depends on: is_int
1206    // *     example 1: \php.input = {4: "red", 'abc': "green", 2: "blue", 'dud': "yellow"};
1207    // *     example 1: \php.array_splice(input, 2);
1208    // *     returns 1: {0: "blue", 'dud': "yellow"}
1209    // *     results 1: input == {'abc':"green", 0:"red"}
1210    // *     example 2: \php.input = ["red", "green", "blue", "yellow"];
1211    // …

Large files files are truncated, but you can click here to view the full file