PHP's strtr in JavaScript

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module.exports = function strtr (str, trFrom, trTo) {
// discuss at: http://locutus.io/php/strtr/
// original by: Brett Zamir (http://brett-zamir.me)
// input by: uestla
// input by: Alan C
// input by: Taras Bogach
// input by: jpfle
// bugfixed by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: Brett Zamir (http://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'
var krsort = require('../array/krsort')
var iniSet = require('../info/ini_set')
var fr = ''
var i = 0
var j = 0
var lenStr = 0
var lenFrom = 0
var sortByReference = false
var fromTypeStr = ''
var toTypeStr = ''
var istr = ''
var tmpFrom = []
var tmpTo = []
var ret = ''
var 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
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/strings/strtr'). You could also require the strings module in full so that you could access strings.strtr instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

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'

« More PHP strings functions