PHP's utf8_decode in JavaScript

Here’s what our current JavaScript equivalent to PHP's utf8_decode 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
module.exports = function utf8_decode (strData) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/utf8_decode/
// original by: Webtoolkit.info (http://www.webtoolkit.info/)
// input by: Aman Gupta
// input by: Brett Zamir (http://brett-zamir.me)
// improved by: Kevin van Zonneveld (http://kvz.io)
// improved by: Norman "zEh" Fuchs
// bugfixed by: hitwork
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// bugfixed by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: kirilloid
// bugfixed by: w35l3y (http://www.wesley.eti.br)
// example 1: utf8_decode('Kevin van Zonneveld')
// returns 1: 'Kevin van Zonneveld'

var tmpArr = []
var i = 0
var c1 = 0
var seqlen = 0

strData += ''

while (i < strData.length) {
c1 = strData.charCodeAt(i) & 0xFF
seqlen = 0

// http://en.wikipedia.org/wiki/UTF-8#Codepage_layout
if (c1 <= 0xBF) {
c1 = (c1 & 0x7F)
seqlen = 1
} else if (c1 <= 0xDF) {
c1 = (c1 & 0x1F)
seqlen = 2
} else if (c1 <= 0xEF) {
c1 = (c1 & 0x0F)
seqlen = 3
} else {
c1 = (c1 & 0x07)
seqlen = 4
}

for (var ai = 1; ai < seqlen; ++ai) {
c1 = ((c1 << 0x06) | (strData.charCodeAt(ai + i) & 0x3F))
}

if (seqlen === 4) {
c1 -= 0x10000
tmpArr.push(String.fromCharCode(0xD800 | ((c1 >> 10) & 0x3FF)))
tmpArr.push(String.fromCharCode(0xDC00 | (c1 & 0x3FF)))
} else {
tmpArr.push(String.fromCharCode(c1))
}

i += seqlen
}

return tmpArr.join('')
}
[ 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/xml/utf8_decode'). You could also require the xml module in full so that you could access xml.utf8_decode 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
1utf8_decode('Kevin van Zonneveld')'Kevin van Zonneveld'

« More PHP xml functions