PHP's strtr in JavaScript

How to use

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

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
1var $trans = {'hello' : 'hi', 'hi' : 'hello'} strtr('hi all, I said hello', $trans)'hello all, I said hi'
2strtr('äaabaåccasdeöoo', 'äåö','aao')'aaabaaccasdeooo'
3strtr('ääääääää', 'ä', 'a')'aaaaaaaa'
4strtr('http', 'pthxyz','xyzpth')'zyyx'
5strtr('zyyx', 'pthxyz','xyzpth')'http'
6strtr('aa', {'a':1,'aa':2})'2'

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

module.exports = function strtr(str, trFrom, trTo) {
// discuss at: https://locutus.io/php/strtr/
// original by: Brett Zamir (https://brett-zamir.me)
// input by: uestla
// input by: Alan C
// input by: Taras Bogach
// input by: jpfle
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// example 1: var $trans = {'hello' : 'hi', 'hi' : 'hello'}
// example 1: strtr('hi all, I said hello', $trans)
// returns 1: 'hello all, I said hi'
// example 2: strtr('äaabaåccasdeöoo', 'äåö','aao')
// returns 2: 'aaabaaccasdeooo'
// example 3: strtr('ääääääää', 'ä', 'a')
// returns 3: 'aaaaaaaa'
// example 4: strtr('http', 'pthxyz','xyzpth')
// returns 4: 'zyyx'
// example 5: strtr('zyyx', 'pthxyz','xyzpth')
// returns 5: 'http'
// example 6: strtr('aa', {'a':1,'aa':2})
// returns 6: '2'

const krsort = require('../array/krsort')
const iniSet = require('../info/ini_set')

let fr = ''
let i = 0
let j = 0
let lenStr = 0
let lenFrom = 0
let sortByReference = false
let fromTypeStr = ''
let toTypeStr = ''
let istr = ''
const tmpFrom = []
const tmpTo = []
let ret = ''
let match = false

// Received replace_pairs?
// Convert to normal trFrom->trTo chars
if (typeof trFrom === 'object') {
// Not thread-safe; temporarily set to true
// @todo: Don't rely on ini here, use internal krsort instead
sortByReference = iniSet('locutus.sortByReference', false)
trFrom = krsort(trFrom)
iniSet('locutus.sortByReference', sortByReference)

for (fr in trFrom) {
if (trFrom.hasOwnProperty(fr)) {
tmpFrom.push(fr)
tmpTo.push(trFrom[fr])
}
}

trFrom = tmpFrom
trTo = tmpTo
}

// Walk through subject and replace chars when needed
lenStr = str.length
lenFrom = trFrom.length
fromTypeStr = typeof trFrom === 'string'
toTypeStr = typeof trTo === 'string'

for (i = 0; i < lenStr; i++) {
match = false
if (fromTypeStr) {
istr = str.charAt(i)
for (j = 0; j < lenFrom; j++) {
if (istr === trFrom.charAt(j)) {
match = true
break
}
}
} else {
for (j = 0; j < lenFrom; j++) {
if (str.substr(i, trFrom[j].length) === trFrom[j]) {
match = true
// Fast forward
i = i + trFrom[j].length - 1
break
}
}
}
if (match) {
ret += toTypeStr ? trTo.charAt(j) : trTo[j]
} else {
ret += str.charAt(i)
}
}

return ret
}

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