3v4l.org

run code in 300+ PHP versions simultaneously
<?php $corrupted_byte_counts = <<<STRING a:12:{i:0;s:3:"three";i:1;s:5:"five";i:2;s:2:"newline1 newline2";i:3;s:6:"garçon";i:4;s:111:"double " quote \"escaped";i:5;s:1:"a,comma";i:6;s:9:"a:colon";i:7;s:0:"single 'quote";i:8;s:999:"semi;colon";s:5:"assoc";s:3:"yes";i:9;s:1:"monkey";wrenching doublequote-semicolon";s:3:"s:";s:9:"val s: val";} STRING; $repaired = preg_replace_callback( '/s:\d+:"(.*?)";(?=$|}|[si]:)/s', // ^^^^^^^^^^^^^^^^^^^-- this extension goes a little further to address a possible monkeywrench function ($m) { return 's:' . strlen($m[1]) . ":\"{$m[1]}\";"; }, $corrupted_byte_counts ); echo "corrupted serialized array:\n$corrupted_byte_counts"; echo "\n---\n"; echo "repaired serialized array:\n$repaired"; echo "\n---\n"; print_r(unserialize($repaired));
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.26, 8.0.0 - 8.0.13, 8.1rc1 - rc3
corrupted serialized array: a:12:{i:0;s:3:"three";i:1;s:5:"five";i:2;s:2:"newline1 newline2";i:3;s:6:"garçon";i:4;s:111:"double " quote \"escaped";i:5;s:1:"a,comma";i:6;s:9:"a:colon";i:7;s:0:"single 'quote";i:8;s:999:"semi;colon";s:5:"assoc";s:3:"yes";i:9;s:1:"monkey";wrenching doublequote-semicolon";s:3:"s:";s:9:"val s: val";} --- repaired serialized array: a:12:{i:0;s:5:"three";i:1;s:4:"five";i:2;s:17:"newline1 newline2";i:3;s:7:"garçon";i:4;s:24:"double " quote \"escaped";i:5;s:7:"a,comma";i:6;s:7:"a:colon";i:7;s:13:"single 'quote";i:8;s:10:"semi;colon";s:5:"assoc";s:3:"yes";i:9;s:39:"monkey";wrenching doublequote-semicolon";s:2:"s:";s:10:"val s: val";} --- Array ( [0] => three [1] => five [2] => newline1 newline2 [3] => garçon [4] => double " quote \"escaped [5] => a,comma [6] => a:colon [7] => single 'quote [8] => semi;colon [assoc] => yes [9] => monkey";wrenching doublequote-semicolon [s:] => val s: val )

preferences:
25.66 ms | 463 KiB | 5 Q