Skip to content

Commit 3d87a7d

Browse files
committed
Update str_ireplace.js
After reading docs on formatting I decided to re-submit this replacement for str_ireplace(). This is a complete rewrite with all new code based on str_replace(). (str_replace() being a clean and efficient PHP 5.0 compliant substitute) This proposed replacement is PHP 5.0 Compliant. Original code does not allow for arrays, does not include optional 'count' and uses regex expressions which are unnecessary and waste of resources. With all due respect to the original author. Sorry for the re-submission but I thought you would like it re-submitted properly. Glen
1 parent 52be837 commit 3d87a7d

File tree

1 file changed

+84
-53
lines changed

1 file changed

+84
-53
lines changed

functions/strings/str_ireplace.js

Lines changed: 84 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,96 @@
1-
function str_ireplace(search, replace, subject) {
1+
function str_ireplace(search, replace, subject, count) {
22
// discuss at: http://phpjs.org/functions/str_ireplace/
3-
// original by: Martijn Wieringa
4-
// input by: penutbutterjelly
5-
// input by: Brett Zamir (http://brett-zamir.me)
6-
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
7-
// improved by: Jack
8-
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
9-
// bugfixed by: Onno Marsman
10-
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
11-
// bugfixed by: Philipp Lenssen
12-
// example 1: str_ireplace('l', 'l', 'HeLLo');
13-
// returns 1: 'Hello'
14-
// example 2: str_ireplace('$', 'foo', '$bar');
15-
// returns 2: 'foobar'
3+
// original by: Glen Arason (http://CanadianDomainRegistry.ca)
4+
// : Case-insensitive version of str_replace()
5+
// : Complient with PHP 5.0 str_ireplace() Full details at:
6+
// : http://ca3.php.net/manual/en/function.str-ireplace.php
7+
// format: str_ireplace($search, $replace, $subject[, 'count'])
8+
// Parameters: If search and replace are arrays, then str_ireplace() takes a
9+
// value from each array and uses them to search and replace on
10+
// subject.
11+
// If replace has fewer values than search, then an empty string
12+
// is used for the rest of replacement values.
13+
// If search is an array and replace is a string, then this
14+
// replacement string is used for every value of search.
15+
// note: The count parameter (optional) if used must be passed in as a
16+
// string. eg global var MyCount:
17+
// str_ireplace($search, $replace, $subject, 'myCount');
18+
// input: str_ireplace($search, $replace, $subject[, {string}]);
19+
// Returns: a string or an array of replacements.
1620

17-
var i, k = '';
18-
var searchl = 0;
19-
var reg;
21+
var i = 0,
22+
j = 0,
23+
temp = '',
24+
repl = '',
25+
sl = 0,
26+
fl = 0,
27+
f = '',
28+
r = '',
29+
s = '',
30+
ra = '',
31+
sa = '',
32+
otemp = '',
33+
oi = '',
34+
ofjl = '',
35+
os = subject,
36+
osa = Object.prototype.toString.call(os) === '[object Array]';
2037

21-
var escapeRegex = function (s) {
22-
return s.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, '\\$1');
23-
};
38+
if(typeof(search) === 'object') {
39+
temp = search;
40+
search = new Array();
41+
for(i=0; i<temp.length;i+=1) {
42+
search[i] = temp[i].toLowerCase();
43+
}
44+
}else { search = search.toLowerCase(); }
2445

25-
search += '';
26-
searchl = search.length;
27-
if (Object.prototype.toString.call(replace) !== '[object Array]') {
28-
replace = [replace];
29-
if (Object.prototype.toString.call(search) === '[object Array]') {
30-
// If search is an array and replace is a string,
31-
// then this replacement string is used for every value of search
32-
while (searchl > replace.length) {
33-
replace[replace.length] = replace[0];
34-
}
46+
if(typeof(subject) === 'object') {
47+
temp = subject;
48+
subject = new Array();
49+
for(i=0; i<temp.length;i+=1) {
50+
subject[i] = temp[i].toLowerCase();
3551
}
36-
}
52+
}else { subject = subject.toLowerCase(); }
3753

38-
if (Object.prototype.toString.call(search) !== '[object Array]') {
39-
search = [search];
54+
if(typeof(search) === 'object' && typeof(replace) === 'string' ) {
55+
temp = replace;
56+
replace = new Array();
57+
for (i=0; i < search.length; i+=1) {
58+
replace[i] = temp;
59+
}
4060
}
41-
while (search.length > replace.length) {
42-
// If replace has fewer values than search,
43-
// then an empty string is used for the rest of replacement values
44-
replace[replace.length] = '';
61+
62+
temp = '';
63+
f = [].concat(search);
64+
r = [].concat(replace);
65+
ra = Object.prototype.toString.call(r) === '[object Array]';
66+
s = subject;
67+
sa = Object.prototype.toString.call(s) === '[object Array]';
68+
s = [].concat(s);
69+
os = [].concat(os);
70+
71+
if (count) {
72+
this.window[count] = 0;
4573
}
4674

47-
if (Object.prototype.toString.call(subject) === '[object Array]') {
48-
// If subject is an array, then the search and replace is performed
49-
// with every entry of subject , and the return value is an array as well.
50-
for (k in subject) {
51-
if (subject.hasOwnProperty(k)) {
52-
subject[k] = str_ireplace(search, replace, subject[k]);
53-
}
75+
for (i = 0, sl = s.length; i < sl; i++) {
76+
if (s[i] === '') {
77+
continue;
5478
}
55-
return subject;
56-
}
79+
for (j = 0, fl = f.length; j < fl; j++) {
80+
temp = s[i] + '';
81+
repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
82+
s[i] = (temp).split(f[j]).join(repl);
83+
otemp = os[i] + '';
84+
oi = temp.indexOf(f[j]);
85+
ofjl = f[j].length;
86+
if(oi >= 0) {
87+
os[i] = (otemp).split(otemp.substr(oi,ofjl)).join(repl);
88+
}
5789

58-
searchl = search.length;
59-
for (i = 0; i < searchl; i++) {
60-
reg = new RegExp(escapeRegex(search[i]), 'gi');
61-
subject = subject.replace(reg, replace[i]);
90+
if (count) {
91+
this.window[count] += ((temp.split(f[j])).length - 1);
92+
}
93+
}
6294
}
63-
64-
return subject;
65-
}
95+
return osa ? os : os[0];
96+
}

0 commit comments

Comments
 (0)