UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

121 lines (103 loc) 3.78 kB
--- layout: page title: "JavaScript str_ireplace function" comments: true sharing: true footer: true alias: - /functions/view/str_ireplace:524 - /functions/view/str_ireplace - /functions/view/524 - /functions/str_ireplace:524 - /functions/524 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's str_ireplace {% codeblock strings/str_ireplace.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/str_ireplace.js raw on github %} function str_ireplace (search, replace, subject) { // From: http://phpjs.org/functions // + original by: Martijn Wieringa // + input by: penutbutterjelly // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + tweaked by: Jack // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Onno Marsman // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Philipp Lenssen // * example 1: str_ireplace('l', 'l', 'HeLLo'); // * returns 1: 'Hello' // * example 2: str_ireplace('$', 'foo', '$bar'); // * returns 2: 'foobar' var i, k = ''; var searchl = 0; var reg; var escapeRegex = function (s) { return s.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, '\\$1'); }; search += ''; searchl = search.length; if (Object.prototype.toString.call(replace) !== '[object Array]') { replace = [replace]; if (Object.prototype.toString.call(search) === '[object Array]') { // If search is an array and replace is a string, // then this replacement string is used for every value of search while (searchl > replace.length) { replace[replace.length] = replace[0]; } } } if (Object.prototype.toString.call(search) !== '[object Array]') { search = [search]; } while (search.length > replace.length) { // If replace has fewer values than search, // then an empty string is used for the rest of replacement values replace[replace.length] = ''; } if (Object.prototype.toString.call(subject) === '[object Array]') { // If subject is an array, then the search and replace is performed // with every entry of subject , and the return value is an array as well. for (k in subject) { if (subject.hasOwnProperty(k)) { subject[k] = str_ireplace(search, replace, subject[k]); } } return subject; } searchl = search.length; for (i = 0; i < searchl; i++) { reg = new RegExp(escapeRegex(search[i]), 'gi'); subject = subject.replace(reg, replace[i]); } return subject; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/str_ireplace.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/str_ireplace.js) ### Example 1 This code {% codeblock lang:js example %} str_ireplace('l', 'l', 'HeLLo'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'Hello' {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} str_ireplace('$', 'foo', '$bar'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'foobar' {% endcodeblock %} ### Other PHP functions in the strings extension {% render_partial _includes/custom/strings.html %}