Install via yarn add locutus and import:
import { str_replace } from 'locutus/php/strings/str_replace'.
Or with CommonJS: const { str_replace } = require('locutus/php/strings/str_replace')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
exportfunctionstr_replace( search: string | string[], replace: string | string[], subject: string | string[], countObj?: CountObj, ): string | string[] { // 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 = asArray(search) let r = asArray(replace) const s = asArray(subject) const sa = Array.isArray(subject)
if (Array.isArray(search) && typeof replace === 'string') { temp = replace constreplaceArr: string[] = [] for (i = 0; i < search.length; i += 1) { replaceArr[i] = temp } temp = '' r = [...replaceArr] }
if (typeof countObj !== 'undefined') { countObj.value = 0 }
exportfunctionstr_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 = asArray(search) let r = asArray(replace) const s = asArray(subject) const sa = Array.isArray(subject)
if (Array.isArray(search) && typeof replace === 'string') { temp = replace const replaceArr = [] for (i = 0; i < search.length; i += 1) { replaceArr[i] = temp } temp = '' r = [...replaceArr] }
if (typeof countObj !== 'undefined') { countObj.value = 0 }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.