Skip to content

Commit

Permalink
Fixed issue #8541: str_replace seems to be replacing just the first o…
Browse files Browse the repository at this point in the history
…ccurrence of search string rather than all occurrences
  • Loading branch information
c-schmitz committed Jan 14, 2014
1 parent 4d1bf0e commit b8ad8cb
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions scripts/expressions/em_javascript.js
Expand Up @@ -303,12 +303,35 @@ function LEMstrlen(a)
return str.length;
}

function LEMstr_replace(needle, replace, haystack)
{
var str = new String(haystack);
return str.replace(needle, replace);
function LEMstr_replace (search, replace, subject) {
// Copied From: http://phpjs.org/functions
var i = 0,
j = 0,
temp = '',
repl = '',
sl = 0,
fl = 0,
f = [].concat(search),
r = [].concat(replace),
s = subject,
ra = Object.prototype.toString.call(r) === '[object Array]',
sa = Object.prototype.toString.call(s) === '[object Array]';
s = [].concat(s);
for (i = 0, sl = s.length; i < sl; i++) {
if (s[i] === '') {
continue;
}
for (j = 0, fl = f.length; j < fl; j++) {
temp = s[i] + '';
repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
s[i] = (temp).split(f[j]).join(repl);
}
}
return sa ? s : s[0];
}



function LEMstrpos(haystack,needle)
{
var str = new String(haystack);
Expand Down

0 comments on commit b8ad8cb

Please sign in to comment.