PHP's utf8_decode in JavaScript

How to use

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

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
1utf8_decode('Kevin van Zonneveld')'Kevin van Zonneveld'

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

module.exports = function utf8_decode(strData) {
// discuss at: https://locutus.io/php/utf8_decode/
// original by: Webtoolkit.info (https://www.webtoolkit.info/)
// input by: Aman Gupta
// input by: Brett Zamir (https://brett-zamir.me)
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Norman "zEh" Fuchs
// bugfixed by: hitwork
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: kirilloid
// bugfixed by: w35l3y (https://www.wesley.eti.br)
// example 1: utf8_decode('Kevin van Zonneveld')
// returns 1: 'Kevin van Zonneveld'

const tmpArr = []
let i = 0
let c1 = 0
let seqlen = 0

strData += ''

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

// https://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 (let 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('')
}

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 xml functions


Star