Unserialize php in Javascript Nodejs
Bogdan Alexandru Militaru
Posted on October 28, 2021
import PhpUnserialize from 'php-unserialize';
const serialized = 'a:0:{}'
const jsObject = PhpUnserialize.unserialize(serialized);
console.log(jsObject) // {}
NPM Library: https://www.npmjs.com/package/php-unserialize
What happens if your serialized string contains special characters ? yeah, it fails!
In order to solve that we can use
import encoding from 'encoding';
export function convertToUtf8Win1252(str) {
return encoding.convert(str, 'WINDOWS-1252').toString();
}
So mixing both functions:
export function unserializePhp(str) {
return PhpUnserialize
.unserialize(convertToUtf8Win1252(str));
}
NPM Library: https://www.npmjs.com/package/encoding
💖 💪 🙅 🚩
Bogdan Alexandru Militaru
Posted on October 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.