DEV Community

Bogdan Alexandru Militaru
Bogdan Alexandru Militaru

Posted on • Originally published at boobo94.github.io on

2 1

Unserialize php in Javascript Nodejs

import PhpUnserialize from 'php-unserialize';

const serialized = 'a:0:{}'
const jsObject = PhpUnserialize.unserialize(serialized);
console.log(jsObject) // {}

Enter fullscreen mode Exit fullscreen mode

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();
}

Enter fullscreen mode Exit fullscreen mode

So mixing both functions:

export function unserializePhp(str) {
  return PhpUnserialize
    .unserialize(convertToUtf8Win1252(str));
}

Enter fullscreen mode Exit fullscreen mode

NPM Library: https://www.npmjs.com/package/encoding

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay