1// functions courtesy of https://locutus.io/ 2 3/* 4the following utility functions have been "namespaced" 5call like: 6phpjs.functionname() 7 8functions list: 9 10base64_decode 11base64_encode 12date 13empty 14explode 15ltrim 16number_format 17parse_str 18rtrim 19str_replace 20stripslashes 21strip_tags 22stripos 23strpos 24trim 25ucwords 26utf8_decode 27utf8_encode 28*/ 29 30// create a "namespace" for the functions 31const phpjs = {} 32 33phpjs.base64_decode = (data) => { 34 // http://kevin.vanzonneveld.net 35 // + original by: Tyler Akins (http://rumkin.com) 36 // + improved by: Thunder.m 37 // + input by: Aman Gupta 38 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 39 // + bugfixed by: Onno Marsman 40 // + bugfixed by: Pellentesque Malesuada 41 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 42 // + input by: Brett Zamir (http://brett-zamir.me) 43 // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 44 // - depends on: utf8_decode 45 // * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA=='); 46 // * returns 1: 'Kevin van Zonneveld' 47 48 // mozilla has this native 49 // - but breaks in 2.0.0.12! 50 //if (typeof this.window['btoa'] == 'function') { 51 // return btoa(data); 52 //} 53 54 var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 55 var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = []; 56 57 if (!data) { 58 return data; 59 } 60 61 data += ''; 62 63 do { // unpack four hexets into three octets using index points in b64 64 h1 = b64.indexOf(data.charAt(i++)); 65 h2 = b64.indexOf(data.charAt(i++)); 66 h3 = b64.indexOf(data.charAt(i++)); 67 h4 = b64.indexOf(data.charAt(i++)); 68 69 bits = h1<<18 | h2<<12 | h3<<6 | h4; 70 71 o1 = bits>>16 & 0xff; 72 o2 = bits>>8 & 0xff; 73 o3 = bits & 0xff; 74 75 if (h3 == 64) { 76 tmp_arr[ac++] = String.fromCharCode(o1); 77 } else if (h4 == 64) { 78 tmp_arr[ac++] = String.fromCharCode(o1, o2); 79 } else { 80 tmp_arr[ac++] = String.fromCharCode(o1, o2, o3); 81 } 82 } while (i < data.length); 83 84 dec = tmp_arr.join(''); 85 dec = this.utf8_decode(dec); 86 87 return dec; 88} 89 90phpjs.base64_encode = (data) => { 91 // http://kevin.vanzonneveld.net 92 // + original by: Tyler Akins (http://rumkin.com) 93 // + improved by: Bayron Guevara 94 // + improved by: Thunder.m 95 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 96 // + bugfixed by: Pellentesque Malesuada 97 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 98 // - depends on: utf8_encode 99 // * example 1: base64_encode('Kevin van Zonneveld'); 100 // * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' 101 102 // mozilla has this native 103 // - but breaks in 2.0.0.12! 104 //if (typeof this.window['atob'] == 'function') { 105 // return atob(data); 106 //} 107 108 var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 109 var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = []; 110 111 if (!data) { 112 return data; 113 } 114 115 data = this.utf8_encode(data+''); 116 117 do { // pack three octets into four hexets 118 o1 = data.charCodeAt(i++); 119 o2 = data.charCodeAt(i++); 120 o3 = data.charCodeAt(i++); 121 122 bits = o1<<16 | o2<<8 | o3; 123 124 h1 = bits>>18 & 0x3f; 125 h2 = bits>>12 & 0x3f; 126 h3 = bits>>6 & 0x3f; 127 h4 = bits & 0x3f; 128 129 // use hexets to index into b64, and append result to encoded string 130 tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4); 131 } while (i < data.length); 132 133 enc = tmp_arr.join(''); 134 135 switch (data.length % 3) { 136 case 1: 137 enc = enc.slice(0, -2) + '=='; 138 break; 139 case 2: 140 enc = enc.slice(0, -1) + '='; 141 break; 142 } 143 144 return enc; 145} 146 147phpjs.date = (format, timestamp) => { 148 // discuss at: https://locutus.io/php/date/ 149 // original by: Carlos R. L. Rodrigues (https://www.jsfromhell.com) 150 // original by: gettimeofday 151 // parts by: Peter-Paul Koch (https://www.quirksmode.org/js/beat.html) 152 // improved by: Kevin van Zonneveld (https://kvz.io) 153 // improved by: MeEtc (https://yass.meetcweb.com) 154 // improved by: Brad Touesnard 155 // improved by: Tim Wiel 156 // improved by: Bryan Elliott 157 // improved by: David Randall 158 // improved by: Theriault (https://github.com/Theriault) 159 // improved by: Theriault (https://github.com/Theriault) 160 // improved by: Brett Zamir (https://brett-zamir.me) 161 // improved by: Theriault (https://github.com/Theriault) 162 // improved by: Thomas Beaucourt (https://www.webapp.fr) 163 // improved by: JT 164 // improved by: Theriault (https://github.com/Theriault) 165 // improved by: RafaÅ Kukawski (https://blog.kukawski.pl) 166 // improved by: Theriault (https://github.com/Theriault) 167 // input by: Brett Zamir (https://brett-zamir.me) 168 // input by: majak 169 // input by: Alex 170 // input by: Martin 171 // input by: Alex Wilson 172 // input by: Haravikk 173 // bugfixed by: Kevin van Zonneveld (https://kvz.io) 174 // bugfixed by: majak 175 // bugfixed by: Kevin van Zonneveld (https://kvz.io) 176 // bugfixed by: Brett Zamir (https://brett-zamir.me) 177 // bugfixed by: omid (https://locutus.io/php/380:380#comment_137122) 178 // bugfixed by: Chris (https://www.devotis.nl/) 179 // note 1: Uses global: locutus to store the default timezone 180 // note 1: Although the function potentially allows timezone info 181 // note 1: (see notes), it currently does not set 182 // note 1: per a timezone specified by date_default_timezone_set(). Implementers might use 183 // note 1: $locutus.currentTimezoneOffset and 184 // note 1: $locutus.currentTimezoneDST set by that function 185 // note 1: in order to adjust the dates in this function 186 // note 1: (or our other date functions!) accordingly 187 // example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400) 188 // returns 1: '07:09:40 m is month' 189 // example 2: date('F j, Y, g:i a', 1062462400) 190 // returns 2: 'September 2, 2003, 12:26 am' 191 // example 3: date('Y W o', 1062462400) 192 // returns 3: '2003 36 2003' 193 // example 4: var $x = date('Y m d', (new Date()).getTime() / 1000) 194 // example 4: $x = $x + '' 195 // example 4: var $result = $x.length // 2009 01 09 196 // returns 4: 10 197 // example 5: date('W', 1104534000) 198 // returns 5: '52' 199 // example 6: date('B t', 1104534000) 200 // returns 6: '999 31' 201 // example 7: date('W U', 1293750000.82); // 2010-12-31 202 // returns 7: '52 1293750000' 203 // example 8: date('W', 1293836400); // 2011-01-01 204 // returns 8: '52' 205 // example 9: date('W Y-m-d', 1293974054); // 2011-01-02 206 // returns 9: '52 2011-01-02'
207 // test: skip-1 skip-2 skip-5 208 209 let jsdate, f 210 // Keep this here (works, but for code commented-out below for file size reasons) 211 // var tal= []; 212 const txtWords = [ 213 'Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur', 214 'January', 'February', 'March', 'April', 'May', 'June', 215 'July', 'August', 'September', 'October', 'November', 'December' 216 ] 217 // trailing backslash -> (dropped) 218 // a backslash followed by any character (including backslash) -> the character 219 // empty string -> empty string 220 const formatChr = /\\?(.?)/gi 221 const formatChrCb = function (t, s) { 222 return f[t] ? f[t]() : s 223 } 224 const _pad = function (n, c) { 225 n = String(n) 226 while (n.length < c) { 227 n = '0' + n 228 } 229 return n 230 } 231 f = { 232 // Day 233 d: function () { 234 // Day of month w/leading 0; 01..31 235 return _pad(f.j(), 2) 236 }, 237 D: function () { 238 // Shorthand day name; Mon...Sun 239 return f.l() 240 .slice(0, 3) 241 }, 242 j: function () { 243 // Day of month; 1..31 244 return jsdate.getDate() 245 }, 246 l: function () { 247 // Full day name; Monday...Sunday 248 return txtWords[f.w()] + 'day' 249 }, 250 N: function () { 251 // ISO-8601 day of week; 1[Mon]..7[Sun] 252 return f.w() || 7 253 }, 254 S: function () { 255 // Ordinal suffix for day of month; st, nd, rd, th 256 const j = f.j() 257 let i = j % 10 258 if (i <= 3 && parseInt((j % 100) / 10, 10) === 1) { 259 i = 0 260 } 261 return ['st', 'nd', 'rd'][i - 1] || 'th' 262 }, 263 w: function () { 264 // Day of week; 0[Sun]..6[Sat] 265 return jsdate.getDay() 266 }, 267 z: function () { 268 // Day of year; 0..365 269 const a = new Date(f.Y(), f.n() - 1, f.j()) 270 const b = new Date(f.Y(), 0, 1) 271 return Math.round((a - b) / 864e5) 272 }, 273 274 // Week 275 W: function () { 276 // ISO-8601 week number 277 const a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3) 278 const b = new Date(a.getFullYear(), 0, 4) 279 return _pad(1 + Math.round((a - b) / 864e5 / 7), 2) 280 }, 281 282 // Month 283 F: function () { 284 // Full month name; January...December 285 return txtWords[6 + f.n()] 286 }, 287 m: function () { 288 // Month w/leading 0; 01...12 289 return _pad(f.n(), 2) 290 }, 291 M: function () { 292 // Shorthand month name; Jan...Dec 293 return f.F() 294 .slice(0, 3) 295 }, 296 n: function () { 297 // Month; 1...12 298 return jsdate.getMonth() + 1 299 }, 300 t: function () { 301 // Days in month; 28...31 302 return (new Date(f.Y(), f.n(), 0)) 303 .getDate() 304 }, 305 306 // Year 307 L: function () { 308 // Is leap year?; 0 or 1 309 const j = f.Y() 310 return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0 311 }, 312 o: function () { 313 // ISO-8601 year 314 const n = f.n() 315 const W = f.W() 316 const Y = f.Y() 317 return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0) 318 }, 319 Y: function () { 320 // Full year; e.g. 1980...2010 321 return jsdate.getFullYear() 322 }, 323 y: function () { 324 // Last two digits of year; 00...99 325 return f.Y() 326 .toString() 327 .slice(-2) 328 }, 329 330 // Time 331 a: function () { 332 // am or pm 333 return jsdate.getHours() > 11 ? 'pm' : 'am' 334 }, 335 A: function () { 336 // AM or PM 337 return f.a() 338 .toUpperCase() 339 }, 340 B: function () { 341 // Swatch Internet time; 000..999 342 const H = jsdate.getUTCHours() * 36e2 343 // Hours 344 const i = jsdate.getUTCMinutes() * 60 345 // Minutes 346 // Seconds 347 const s = jsdate.getUTCSeconds() 348 return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3) 349 }, 350 g: function () { 351 // 12-Hours; 1..12 352 return f.G() % 12 || 12 353 }, 354 G: function () { 355 // 24-Hours; 0..23 356 return jsdate.getHours() 357 }, 358 h: function () { 359 // 12-Hours w/leading 0; 01..12 360 return _pad(f.g(), 2) 361 }, 362 H: function () { 363 // 24-Hours w/leading 0; 00..23 364 return _pad(f.G(), 2) 365 }, 366 i: function () { 367 // Minutes w/leading 0; 00..59 368 return _pad(jsdate.getMinutes(), 2) 369 }, 370 s: function () { 371 // Seconds w/leading 0; 00..59 372 return _pad(jsdate.getSeconds(), 2) 373 }, 374 u: function () { 375 // Microseconds; 000000-999000 376 return _pad(jsdate.getMilliseconds() * 1000, 6) 377 }, 378 379 // Timezone 380 e: function () { 381 // Timezone identifier; e.g. Atlantic/Azores, ... 382 // The following works, but requires inclusion of the very large 383 // timezone_abbreviations_list() function. 384 /* return that.date_default_timezone_get(); 385 */ 386 const msg = 'Not supported (see source code of date() for timezone on how to add support)' 387 throw new Error(msg) 388 }, 389 I: function () { 390 // DST observed?; 0 or 1 391 // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC. 392 // If they are not equal, then DST is observed. 393 const a = new Date(f.Y(), 0) 394 // Jan 1 395 const c = Date.UTC(f.Y(), 0) 396 // Jan 1 UTC 397 const b = new Date(f.Y(), 6) 398 // Jul 1 399 // Jul 1 UTC 400 const d = Date.UTC(f.Y(), 6) 401 return ((a - c) !== (b - d)) ? 1 : 0 402 }, 403 O: function () { 404 // Difference to GMT in hour format; e.g. +0200 405 const tzo = jsdate.getTimezoneOffset() 406 const a = Math.abs(tzo) 407 return (tzo > 0 ? '-' : '+') + _pad(Math.floor(a / 60) * 100 + a % 60, 4) 408 }, 409 P: function () { 410 // Difference to GMT w/colon; e.g. +02:00 411 const O = f.O() 412 return (O.substr(0, 3) + ':' + O.substr(3, 2)) 413 }, 414 T: function () { 415 // The following works, but requires inclusion of the very 416 // large timezone_abbreviations_list() function. 417 /* var abbr, i, os, _default; 418 if (!tal.length) { 419 tal = that.timezone_abbreviations_list(); 420 } 421 if ($locutus && $locutus.default_timezone) { 422 _default = $locutus.default_timezone; 423 for (abbr in tal) { 424 for (i = 0; i < tal[abbr].length; i++) { 425 if (tal[abbr][i].timezone_id === _default) { 426 return abbr.toUpperCase(); 427 } 428 } 429 } 430 } 431 for (abbr in tal) { 432 for (i = 0; i < tal[abbr].length; i++) { 433 os = -jsdate.getTimezoneOffset() * 60; 434 if (tal[abbr][i].offset === os) { 435 return abbr.toUpperCase(); 436 } 437 } 438 } 439 */ 440 return 'UTC' 441 }, 442 Z: function () { 443 // Timezone offset in seconds (-43200...50400) 444 return -jsdate.getTimezoneOffset() * 60 445 }, 446 447 // Full Date/Time 448 c: function () { 449 // ISO-8601 date. 450 return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb) 451 }, 452 r: function () { 453 // RFC 2822 454 return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb) 455 }, 456 U: function () { 457 // Seconds since UNIX epoch 458 return jsdate / 1000 | 0 459 } 460 } 461 462 const _date = function (format, timestamp) { 463 jsdate = (timestamp === undefined 464 ? new Date() // Not provided 465 : (timestamp instanceof Date) 466 ? new Date(timestamp) // JS Date() 467 : new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int) 468 ) 469 return format.replace(formatChr, formatChrCb) 470 } 471 472 return _date(format, timestamp) 473} 474 475phpjs.empty = (mixedVar) => { 476 // discuss at: http://locutus.io/php/empty/ 477 // original by: Philippe Baumann 478 // input by: Onno Marsman (https://twitter.com/onnomarsman) 479 // input by: LH 480 // input by: Stoyan Kyosev (http://www.svest.org/) 481 // bugfixed by: Kevin van Zonneveld (http://kvz.io) 482 // improved by: Onno Marsman (https://twitter.com/onnomarsman) 483 // improved by: Francesco 484 // improved by: Marc Jansen 485 // improved by: Rafal Kukawski (http://blog.kukawski.pl) 486 // example 1: empty(null) 487 // returns 1: true 488 // example 2: empty(undefined) 489 // returns 2: true 490 // example 3: empty([]) 491 // returns 3: true 492 // example 4: empty({}) 493 // returns 4: true 494 // example 5: empty({'aFunc' : function () { alert('humpty'); } }) 495 // returns 5: false 496 var undef 497 var key 498 var i 499 var len 500 var emptyValues = [undef, null, false, 0, '', '0'] 501 for (i = 0, len = emptyValues.length; i < len; i++) { 502 if (mixedVar === emptyValues[i]) { 503 return true 504 } 505 } 506 if (typeof mixedVar === 'object') { 507 for (key in mixedVar) { 508 if (mixedVar.hasOwnProperty(key)) { 509 return false 510 } 511 } 512 return true 513 } 514 return false 515} 516 517 518phpjs.explode = (delimiter, string, limit) => { 519 // discuss at: http://locutus.io/php/explode/ 520 // original by: Kevin van Zonneveld (http://kvz.io) 521 // example 1: explode(' ', 'Kevin van Zonneveld') 522 // returns 1: [ 'Kevin', 'van', 'Zonneveld' ] 523 524 if (arguments.length < 2 || 525 typeof delimiter === 'undefined' || 526 typeof string === 'undefined') { 527 return null 528 } 529 if (delimiter === '' || 530 delimiter === false || 531 delimiter === null) { 532 return false 533 } 534 if (typeof delimiter === 'function' || 535 typeof delimiter === 'object' || 536 typeof string === 'function' || 537 typeof string === 'object') { 538 return { 539 0: '' 540 } 541 } 542 if (delimiter === true) { 543 delimiter = '1' 544 } 545 546 // Here we go... 547 delimiter += '' 548 string += '' 549 550 var s = string.split(delimiter) 551 552 if (typeof limit === 'undefined') return s 553
554 // Support for limit 555 if (limit === 0) limit = 1 556 557 // Positive limit 558 if (limit > 0) { 559 if (limit >= s.length) { 560 return s 561 } 562 return s 563 .slice(0, limit - 1) 564 .concat([s.slice(limit - 1) 565 .join(delimiter) 566 ]) 567 } 568 569 // Negative limit 570 if (-limit >= s.length) { 571 return [] 572 } 573 574 s.splice(s.length + limit) 575 return s 576} 577 578phpjs.ltrim = ( str, charlist ) => { 579 // http://kevin.vanzonneveld.net 580 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 581 // + input by: Erkekjetter 582 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 583 // + bugfixed by: Onno Marsman 584 // * example 1: ltrim(' Kevin van Zonneveld '); 585 // * returns 1: 'Kevin van Zonneveld ' 586 587 charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1'); 588 var re = new RegExp('^[' + charlist + ']+', 'g'); 589 return (str+'').replace(re, ''); 590} 591 592phpjs.number_format = (number, decimals, decPoint, thousandsSep) => { // eslint-disable-line camelcase 593 // discuss at: http://locutus.io/php/number_format/ 594 // original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) 595 // improved by: Kevin van Zonneveld (http://kvz.io) 596 // improved by: davook 597 // improved by: Brett Zamir (http://brett-zamir.me) 598 // improved by: Brett Zamir (http://brett-zamir.me) 599 // improved by: Theriault (https://github.com/Theriault) 600 // improved by: Kevin van Zonneveld (http://kvz.io) 601 // bugfixed by: Michael White (http://getsprink.com) 602 // bugfixed by: Benjamin Lupton 603 // bugfixed by: Allan Jensen (http://www.winternet.no) 604 // bugfixed by: Howard Yeend 605 // bugfixed by: Diogo Resende 606 // bugfixed by: Rival 607 // bugfixed by: Brett Zamir (http://brett-zamir.me) 608 // revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) 609 // revised by: Luke Smith (http://lucassmith.name) 610 // input by: Kheang Hok Chin (http://www.distantia.ca/) 611 // input by: Jay Klehr 612 // input by: Amir Habibi (http://www.residence-mixte.com/) 613 // input by: Amirouche 614 // example 1: number_format(1234.56) 615 // returns 1: '1,235' 616 // example 2: number_format(1234.56, 2, ',', ' ') 617 // returns 2: '1 234,56' 618 // example 3: number_format(1234.5678, 2, '.', '') 619 // returns 3: '1234.57' 620 // example 4: number_format(67, 2, ',', '.') 621 // returns 4: '67,00' 622 // example 5: number_format(1000) 623 // returns 5: '1,000' 624 // example 6: number_format(67.311, 2) 625 // returns 6: '67.31' 626 // example 7: number_format(1000.55, 1) 627 // returns 7: '1,000.6' 628 // example 8: number_format(67000, 5, ',', '.') 629 // returns 8: '67.000,00000' 630 // example 9: number_format(0.9, 0) 631 // returns 9: '1' 632 // example 10: number_format('1.20', 2) 633 // returns 10: '1.20' 634 // example 11: number_format('1.20', 4) 635 // returns 11: '1.2000' 636 // example 12: number_format('1.2000', 3) 637 // returns 12: '1.200' 638 // example 13: number_format('1 000,50', 2, '.', ' ') 639 // returns 13: '100 050.00' 640 // example 14: number_format(1e-8, 8, '.', '') 641 // returns 14: '0.00000001' 642 643 number = (number + '').replace(/[^0-9+\-Ee.]/g, '') 644 var n = !isFinite(+number) ? 0 : +number 645 var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals) 646 var sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep 647 var dec = (typeof decPoint === 'undefined') ? '.' : decPoint 648 var s = '' 649 650 var toFixedFix = function (n, prec) { 651 var k = Math.pow(10, prec) 652 return '' + (Math.round(n * k) / k) 653 .toFixed(prec) 654 } 655 656 // @todo: for IE parseFloat(0.55).toFixed(0) = 0; 657 s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.') 658 if (s[0].length > 3) { 659 s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep) 660 } 661 if ((s[1] || '').length < prec) { 662 s[1] = s[1] || '' 663 s[1] += new Array(prec - s[1].length + 1).join('0') 664 } 665 666 return s.join(dec) 667} 668 669phpjs.parse_str = (str, array) => { // eslint-disable-line camelcase 670 // discuss at: http://locutus.io/php/parse_str/ 671 // original by: Cagri Ekin 672 // improved by: Michael White (http://getsprink.com) 673 // improved by: Jack 674 // improved by: Brett Zamir (http://brett-zamir.me) 675 // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) 676 // bugfixed by: Brett Zamir (http://brett-zamir.me) 677 // bugfixed by: stag019 678 // bugfixed by: Brett Zamir (http://brett-zamir.me) 679 // bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/) 680 // reimplemented by: stag019 681 // input by: Dreamer 682 // input by: Zaide (http://zaidesthings.com/) 683 // input by: David Pesta (http://davidpesta.com/) 684 // input by: jeicquest 685 // note 1: When no argument is specified, will put variables in global scope. 686 // note 1: When a particular argument has been passed, and the 687 // note 1: returned value is different parse_str of PHP. 688 // note 1: For example, a=b=c&d====c 689 // example 1: var $arr = {} 690 // example 1: parse_str('first=foo&second=bar', $arr) 691 // example 1: var $result = $arr 692 // returns 1: { first: 'foo', second: 'bar' } 693 // example 2: var $arr = {} 694 // example 2: parse_str('str_a=Jack+and+Jill+didn%27t+see+the+well.', $arr) 695 // example 2: var $result = $arr 696 // returns 2: { str_a: "Jack and Jill didn't see the well." } 697 // example 3: var $abc = {3:'a'} 698 // example 3: parse_str('a[b]["c"]=def&a[q]=t+5', $abc) 699 // example 3: var $result = $abc 700 // returns 3: {"3":"a","a":{"b":{"c":"def"},"q":"t 5"}} 701 var strArr = String(str).replace(/^&/, '').replace(/&$/, '').split('&') 702 var sal = strArr.length 703 var i 704 var j 705 var ct 706 var p 707 var lastObj 708 var obj 709 var undef 710 var chr 711 var tmp 712 var key 713 var value 714 var postLeftBracketPos 715 var keys 716 var keysLen 717 var _fixStr = function (str) { 718 return decodeURIComponent(str.replace(/\+/g, '%20')) 719 } 720 var $global = (typeof window !== 'undefined' ? window : global) 721 $global.$locutus = $global.$locutus || {} 722 var $locutus = $global.$locutus 723 $locutus.php = $locutus.php || {} 724 if (!array) { 725 array = $global 726 } 727 for (i = 0; i < sal; i++) { 728 tmp = strArr[i].split('=') 729 key = _fixStr(tmp[0]) 730 value = (tmp.length < 2) ? '' : _fixStr(tmp[1]) 731 while (key.charAt(0) === ' ') { 732 key = key.slice(1) 733 } 734 if (key.indexOf('\x00') > -1) { 735 key = key.slice(0, key.indexOf('\x00')) 736 } 737 if (key && key.charAt(0) !== '[') { 738 keys = [] 739 postLeftBracketPos = 0 740 for (j = 0; j < key.length; j++) { 741 if (key.charAt(j) === '[' && !postLeftBracketPos) { 742 postLeftBracketPos = j + 1 743 } else if (key.charAt(j) === ']') { 744 if (postLeftBracketPos) { 745 if (!keys.length) { 746 keys.push(key.slice(0, postLeftBracketPos - 1)) 747 } 748 keys.push(key.substr(postLeftBracketPos, j - postLeftBracketPos)) 749 postLeftBracketPos = 0 750 if (key.charAt(j + 1) !== '[') { 751 break 752 } 753 } 754 } 755 } 756 if (!keys.length) { 757 keys = [key] 758 } 759 for (j = 0; j < keys[0].length; j++) {
760 chr = keys[0].charAt(j) 761 if (chr === ' ' || chr === '.' || chr === '[') { 762 keys[0] = keys[0].substr(0, j) + '_' + keys[0].substr(j + 1) 763 } 764 if (chr === '[') { 765 break 766 } 767 } 768 obj = array 769 for (j = 0, keysLen = keys.length; j < keysLen; j++) { 770 key = keys[j].replace(/^['"]/, '').replace(/['"]$/, '') 771 lastObj = obj 772 if ((key !== '' && key !== ' ') || j === 0) { 773 if (obj[key] === undef) { 774 obj[key] = {} 775 } 776 obj = obj[key] 777 } else { 778 // To insert new dimension 779 ct = -1 780 for (p in obj) { 781 if (obj.hasOwnProperty(p)) { 782 if (+p > ct && p.match(/^\d+$/g)) { 783 ct = +p 784 } 785 } 786 } 787 key = ct + 1 788 } 789 } 790 lastObj[key] = value 791 } 792 } 793} 794 795phpjs.rtrim = ( str, charlist ) => { 796 // http://kevin.vanzonneveld.net 797 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 798 // + input by: Erkekjetter 799 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 800 // + bugfixed by: Onno Marsman 801 // + input by: rem 802 // + bugfixed by: Brett Zamir (http://brett-zamir.me) 803 // * example 1: rtrim(' Kevin van Zonneveld '); 804 // * returns 1: ' Kevin van Zonneveld' 805 806 charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1'); 807 var re = new RegExp('[' + charlist + ']+$', 'g'); 808 return (str+'').replace(re, ''); 809} 810 811phpjs.str_replace = (search, replace, subject, count) => { 812 // http://kevin.vanzonneveld.net 813 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 814 // + improved by: Gabriel Paderni 815 // + improved by: Philip Peterson 816 // + improved by: Simon Willison (http://simonwillison.net) 817 // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) 818 // + bugfixed by: Anton Ongson 819 // + input by: Onno Marsman 820 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 821 // + tweaked by: Onno Marsman 822 // + input by: Brett Zamir (http://brett-zamir.me) 823 // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 824 // + input by: Oleg Eremeev 825 // + improved by: Brett Zamir (http://brett-zamir.me) 826 // + bugfixed by: Oleg Eremeev 827 // % note 1: The count parameter must be passed as a string in order 828 // % note 1: to find a global variable in which the result will be given 829 // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld'); 830 // * returns 1: 'Kevin.van.Zonneveld' 831 // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars'); 832 // * returns 2: 'hemmo, mars' 833 834 var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0, 835 f = [].concat(search), 836 r = [].concat(replace), 837 s = subject, 838 ra = r instanceof Array, sa = s instanceof Array; 839 s = [].concat(s); 840 if (count) { 841 this.window[count] = 0; 842 } 843 844 for (i=0, sl=s.length; i < sl; i++) { 845 if (s[i] === '') { 846 continue; 847 } 848 for (j=0, fl=f.length; j < fl; j++) { 849 temp = s[i]+''; 850 repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0]; 851 s[i] = (temp).split(f[j]).join(repl); 852 if (count && s[i] !== temp) { 853 this.window[count] += (temp.length-s[i].length)/f[j].length;} 854 } 855 } 856 return sa ? s : s[0]; 857} 858 859phpjs.stripslashes = (str) => { 860 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 861 // + improved by: Ates Goral (http://magnetiq.com) 862 // + fixed by: Mick@el 863 // + improved by: marrtins 864 // + bugfixed by: Onno Marsman 865 // + improved by: rezna 866 // + input by: Rick Waldron 867 // + reimplemented by: Brett Zamir (http://brett-zamir.me) 868 // + input by: Brant Messenger (http://www.brantmessenger.com/) 869 // + bugfixed by: Brett Zamir (http://brett-zamir.me) 870 // * example 1: stripslashes('Kevin\'s code'); 871 // * returns 1: "Kevin's code" 872 // * example 2: stripslashes('Kevin\\\'s code'); 873 // * returns 2: "Kevin\'s code" 874 return (str + '').replace(/\\(.?)/g, function (s, n1) { 875 switch (n1) { 876 case '\\': 877 return '\\'; 878 case '0': 879 return '\u0000'; 880 case '': 881 return ''; 882 default: 883 return n1; 884 } 885 }); 886} 887 888phpjs.strip_tags = (input, allowed) => { // eslint-disable-line camelcase 889 // discuss at: http://locutus.io/php/strip_tags/ 890 // original by: Kevin van Zonneveld (http://kvz.io) 891 // improved by: Luke Godfrey 892 // improved by: Kevin van Zonneveld (http://kvz.io) 893 // input by: Pul 894 // input by: Alex 895 // input by: Marc Palau 896 // input by: Brett Zamir (http://brett-zamir.me) 897 // input by: Bobby Drake 898 // input by: Evertjan Garretsen 899 // bugfixed by: Kevin van Zonneveld (http://kvz.io) 900 // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) 901 // bugfixed by: Kevin van Zonneveld (http://kvz.io) 902 // bugfixed by: Kevin van Zonneveld (http://kvz.io) 903 // bugfixed by: Eric Nagel 904 // bugfixed by: Kevin van Zonneveld (http://kvz.io) 905 // bugfixed by: Tomasz Wesolowski 906 // revised by: Rafal Kukawski (http://blog.kukawski.pl) 907 // example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>') 908 // returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>' 909 // example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>') 910 // returns 2: '<p>Kevin van Zonneveld</p>' 911 // example 3: strip_tags("<a href='http://kvz.io'>Kevin van Zonneveld</a>", "<a>") 912 // returns 3: "<a href='http://kvz.io'>Kevin van Zonneveld</a>" 913 // example 4: strip_tags('1 < 5 5 > 1') 914 // returns 4: '1 < 5 5 > 1'
915 // example 5: strip_tags('1 <br/> 1') 916 // returns 5: '1 1' 917 // example 6: strip_tags('1 <br/> 1', '<br>') 918 // returns 6: '1 <br/> 1' 919 // example 7: strip_tags('1 <br/> 1', '<br><br/>') 920 // returns 7: '1 <br/> 1' 921 // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>) 922 allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('') 923 var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi 924 var commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi 925 return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) { 926 return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '' 927 }) 928} 929 930phpjs.stripos = (fHaystack, fNeedle, fOffset) => { 931 // discuss at: http://locutus.io/php/stripos/ 932 // original by: Martijn Wieringa 933 // revised by: Onno Marsman (https://twitter.com/onnomarsman) 934 // example 1: stripos('ABC', 'a') 935 // returns 1: 0 936 937 var haystack = (fHaystack + '').toLowerCase() 938 var needle = (fNeedle + '').toLowerCase() 939 var index = 0 940 941 if ((index = haystack.indexOf(needle, fOffset)) !== -1) { 942 return index 943 } 944 945 return false 946} 947 948phpjs.strpos = (haystack, needle, offset) => { 949 // discuss at: http://locutus.io/php/strpos/ 950 // original by: Kevin van Zonneveld (http://kvz.io) 951 // improved by: Onno Marsman (https://twitter.com/onnomarsman) 952 // improved by: Brett Zamir (http://brett-zamir.me) 953 // bugfixed by: Daniel Esteban 954 // example 1: strpos('Kevin van Zonneveld', 'e', 5) 955 // returns 1: 14 956 957 var i = (haystack + '') 958 .indexOf(needle, (offset || 0)) 959 return i === -1 ? false : i 960} 961 962phpjs.trim = (str, charlist) => { 963 // http://kevin.vanzonneveld.net 964 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 965 // + improved by: mdsjack (http://www.mdsjack.bo.it) 966 // + improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev) 967 // + input by: Erkekjetter 968 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 969 // + input by: DxGx 970 // + improved by: Steven Levithan (http://blog.stevenlevithan.com) 971 // + tweaked by: Jack 972 // + bugfixed by: Onno Marsman 973 // * example 1: trim(' Kevin van Zonneveld '); 974 // * returns 1: 'Kevin van Zonneveld' 975 // * example 2: trim('Hello World', 'Hdle'); 976 // * returns 2: 'o Wor' 977 // * example 3: trim(16, 1); 978 // * returns 3: 6 979 980 var whitespace, l = 0, i = 0; 981 str += ''; 982 983 if (!charlist) { 984 // default list 985 whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000"; 986 } else { 987 // preg_quote custom list 988 charlist += ''; 989 whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1'); 990 } 991 992 l = str.length; 993 for (i = 0; i < l; i++) { 994 if (whitespace.indexOf(str.charAt(i)) === -1) { 995 str = str.substring(i); 996 break; 997 } 998 } 999 1000 l = str.length; 1001 for (i = l - 1; i >= 0; i--) { 1002 if (whitespace.indexOf(str.charAt(i)) === -1) { 1003 str = str.substring(0, i + 1); 1004 break; 1005 } 1006 } 1007 1008 return whitespace.indexOf(str.charAt(0)) === -1 ? str : ''; 1009} 1010 1011phpjs.ucwords = (str) => { 1012 // discuss at: http://phpjs.org/functions/ucwords/ 1013 // original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) 1014 // improved by: Waldo Malqui Silva (http://waldo.malqui.info) 1015 // improved by: Robin 1016 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 1017 // bugfixed by: Onno Marsman 1018 // input by: James (http://www.james-bell.co.uk/) 1019 // example 1: ucwords('kevin van zonneveld'); 1020 // returns 1: 'Kevin Van Zonneveld' 1021 // example 2: ucwords('HELLO WORLD'); 1022 // returns 2: 'HELLO WORLD' 1023 1024 return (str + '') 1025 .replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function ($1) { 1026 return $1.toUpperCase(); 1027 });
1028} 1029 1030phpjs.utf8_decode = ( str_data ) => { 1031 // http://kevin.vanzonneveld.net 1032 // + original by: Webtoolkit.info (http://www.webtoolkit.info/) 1033 // + input by: Aman Gupta 1034 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 1035 // + improved by: Norman "zEh" Fuchs 1036 // + bugfixed by: hitwork 1037 // + bugfixed by: Onno Marsman 1038 // + input by: Brett Zamir (http://brett-zamir.me) 1039 // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 1040 // * example 1: utf8_decode('Kevin van Zonneveld'); 1041 // * returns 1: 'Kevin van Zonneveld' 1042 1043 var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0; 1044 1045 str_data += ''; 1046 1047 while ( i < str_data.length ) { 1048 c1 = str_data.charCodeAt(i); 1049 if (c1 < 128) { 1050 tmp_arr[ac++] = String.fromCharCode(c1); 1051 i++; 1052 } else if ((c1 > 191) && (c1 < 224)) { 1053 c2 = str_data.charCodeAt(i+1); 1054 tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63)); 1055 i += 2; 1056 } else { 1057 c2 = str_data.charCodeAt(i+1); 1058 c3 = str_data.charCodeAt(i+2); 1059 tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 1060 i += 3; 1061 } 1062 } 1063 1064 return tmp_arr.join(''); 1065} 1066 1067phpjs.utf8_encode = ( argString ) => { 1068 // http://kevin.vanzonneveld.net 1069 // + original by: Webtoolkit.info (http://www.webtoolkit.info/) 1070 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 1071 // + improved by: sowberry 1072 // + tweaked by: Jack 1073 // + bugfixed by: Onno Marsman 1074 // + improved by: Yves Sucaet 1075 // + bugfixed by: Onno Marsman 1076 // + bugfixed by: Ulrich 1077 // * example 1: utf8_encode('Kevin van Zonneveld'); 1078 // * returns 1: 'Kevin van Zonneveld' 1079 1080 var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n"); 1081 1082 var utftext = ""; 1083 var start, end; 1084 var stringl = 0; 1085 1086 start = end = 0; 1087 stringl = string.length; 1088 for (var n = 0; n < stringl; n++) { 1089 var c1 = string.charCodeAt(n); 1090 var enc = null; 1091 1092 if (c1 < 128) { 1093 end++; 1094 } else if (c1 > 127 && c1 < 2048) { 1095 enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128); 1096 } else { 1097 enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128); 1098 } 1099 if (enc !== null) { 1100 if (end > start) { 1101 utftext += string.substring(start, end); 1102 } 1103 utftext += enc; 1104 start = end = n+1; 1105 } 1106 } 1107 1108 if (end > start) { 1109 utftext += string.substring(start, string.length); 1110 } 1111 1112 return utftext; 1113}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.