Skip to content

Commit f415314

Browse files
committed
Update str_replace.js
php quote: "If search is an array and replace is a string, then this replacement string is used for every value of search." This mod converts replace to an Array of search.length
1 parent 513a199 commit f415314

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

functions/strings/str_replace.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function str_replace(search, replace, subject, count) {
2020
// returns 1: 'Kevin.van.Zonneveld'
2121
// example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
2222
// returns 2: 'hemmo, mars'
23+
// bugfixed by: Glen Arason (http://CanadianDomainRegistry.ca)
24+
// example 3: str_replace(Array('S','F'),'x','ASDFASDF');
25+
// returns 3: 'AxDxAxDx'
2326

2427
var i = 0,
2528
j = 0,
@@ -33,6 +36,14 @@ function str_replace(search, replace, subject, count) {
3336
ra = Object.prototype.toString.call(r) === '[object Array]',
3437
sa = Object.prototype.toString.call(s) === '[object Array]';
3538
s = [].concat(s);
39+
40+
if(typeof(search) == 'object' && typeof(replace) == 'string' ) {
41+
temp = replace; replace = new Array();
42+
for (i=0; i < search.length; i+=1) { replace[i] = temp; }
43+
// update vars
44+
temp = ''; r = [].concat(replace); ra = Object.prototype.toString.call(r) === '[object Array]';
45+
}
46+
3647
if (count) {
3748
this.window[count] = 0;
3849
}
@@ -53,4 +64,4 @@ function str_replace(search, replace, subject, count) {
5364
}
5465
}
5566
return sa ? s : s[0];
56-
}
67+
}

0 commit comments

Comments
 (0)