PHP's str_replace in JavaScript

How to use

You you can install via yarn add locutus and require this function via const str_replace = require('locutus/php/strings/str_replace').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1str_replace(' ', '.', 'Kevin van Zonneveld')'Kevin.van.Zonneveld'
2str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars')'hemmo, mars'
3str_replace(Array('S','F'),'x','ASDFASDF')'AxDxAxDx'
4var countObj = {} str_replace(['A','D'], ['x','y'] , 'ASDFASDF' , countObj) var $result = countObj.value4
5str_replace('', '.', 'aaa')'aaa'

Notes

  • The countObj parameter (optional) if used must be passed in as a object. The count will then be written by reference into it’s value property

Here’s what our current JavaScript equivalent to PHP's str_replace looks like.

module.exports = function str_replace(search, replace, subject, countObj) {
// discuss at: https://locutus.io/php/str_replace/
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: Gabriel Paderni
// improved by: Philip Peterson
// improved by: Simon Willison (https://simonwillison.net)
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Brett Zamir (https://brett-zamir.me)
// revised by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// bugfixed by: Anton Ongson
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Oleg Eremeev
// bugfixed by: Glen Arason (https://CanadianDomainRegistry.ca)
// bugfixed by: Glen Arason (https://CanadianDomainRegistry.ca)
// bugfixed by: Mahmoud Saeed
// input by: Onno Marsman (https://twitter.com/onnomarsman)
// input by: Brett Zamir (https://brett-zamir.me)
// input by: Oleg Eremeev
// note 1: The countObj parameter (optional) if used must be passed in as a
// note 1: object. The count will then be written by reference into it's `value` property
// example 1: str_replace(' ', '.', 'Kevin van Zonneveld')
// returns 1: 'Kevin.van.Zonneveld'
// example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars')
// returns 2: 'hemmo, mars'
// example 3: str_replace(Array('S','F'),'x','ASDFASDF')
// returns 3: 'AxDxAxDx'
// example 4: var countObj = {}
// example 4: str_replace(['A','D'], ['x','y'] , 'ASDFASDF' , countObj)
// example 4: var $result = countObj.value
// returns 4: 4
// example 5: str_replace('', '.', 'aaa')
// returns 5: 'aaa'

let i = 0
let j = 0
let temp = ''
let repl = ''
let sl = 0
let fl = 0
const f = [].concat(search)
let r = [].concat(replace)
let s = subject
let ra = Object.prototype.toString.call(r) === '[object Array]'
const sa = Object.prototype.toString.call(s) === '[object Array]'
s = [].concat(s)

const $global = typeof window !== 'undefined' ? window : global
$global.$locutus = $global.$locutus || {}
const $locutus = $global.$locutus
$locutus.php = $locutus.php || {}

if (typeof search === 'object' && typeof replace === 'string') {
temp = replace
replace = []
for (i = 0; i < search.length; i += 1) {
replace[i] = temp
}
temp = ''
r = [].concat(replace)
ra = Object.prototype.toString.call(r) === '[object Array]'
}

if (typeof countObj !== 'undefined') {
countObj.value = 0
}

for (i = 0, sl = s.length; i < sl; i++) {
if (s[i] === '') {
continue
}
for (j = 0, fl = f.length; j < fl; j++) {
if (f[j] === '') {
continue
}
temp = s[i] + ''
repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0]
s[i] = temp.split(f[j]).join(repl)
if (typeof countObj !== 'undefined') {
countObj.value += temp.split(f[j]).length - 1
}
}
}
return sa ? s : s[0]
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP strings functions


Star