The PHP to JSON converter deserializes PHP data formats into JSON. Supports both PHP serialize format and PHP array syntax, with proper handling of nested structures, objects, and type preservation.
export function phpUnserialize(input: string): PhpValue { const text = input.trim(); let pos = 0; function parse(): PhpValue { const type = text[pos++]; if (pos < text.length && text[pos] === ':') pos++; switch (type) { case 'N': return null; case 'b': return readUntil(';') === '1'; case 'i': return parseInt(readUntil(';'), 10); case 'd': return parseFloat(readUntil(';')); case 's': return readString(); case 'a': return readArray(); case 'O': return readObject(); default: throw new Error(`Unknown type '${type}'`); } } return parse();}
export function parsePhpArraySyntax(input: string): PhpValue { const text = input.trim(); let pos = 0; function parseVal(): PhpValue { skip(); const ch = text[pos]; // Keywords if (text.startsWith('null', pos)) { pos += 4; return null; } if (text.startsWith('true', pos)) { pos += 4; return true; } if (text.startsWith('false', pos)) { pos += 5; return false; } // Numbers if (ch === '-' || (ch >= '0' && ch <= '9')) return parseNumber(); // Strings if (ch === "'" || ch === '"') return parseString(); // Arrays if (ch === '[' || text.startsWith('array(', pos)) return parseArray(); throw new Error(`Unexpected token at position ${pos}`); } return parseVal();}
The PHP to JSON converter handles both modern short array syntax ([]) and legacy array() function syntax. Objects are converted to JSON objects with a special __class property preserving the class name.
For PHP session data, use the default (serialize) mode. For config files and code snippets with array syntax, use the “From Array” action.
The unserializer does not execute PHP code or instantiate classes
Private/protected properties in serialized objects are converted to regular properties
Custom serialization handlers (like __sleep, __wakeup) are not invoked
Very deeply nested structures may cause stack overflow