3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function remove_recursion($o){ static $replace; if(!isset($replace)) $replace = create_function('$m','$r="\x00{$m[1]}ecursion_";return \'s:\'.strlen($r.$m[2]).\':"\'.$r.$m[2].\'";\';') ; if(is_array($o) || is_object($o)){ $re = '#(r|R):([0-9]+);#'; $serialize = serialize($o); if(preg_match($re, $serialize)){ $last = $pos = 0; while(false !== ($pos = strpos($serialize, 's:', $pos))){ $chunk = substr($serialize, $last, $pos - $last); if(preg_match($re, $chunk)){ $length = strlen($chunk); $chunk = preg_replace_callback($re, $replace, $chunk); $serialize = substr($serialize, 0, $last).$chunk.substr($serialize, $last + ($pos - $last)); $pos += strlen($chunk) - $length; } $pos += 2; $last = strpos($serialize, ':', $pos); $length = substr($serialize, $pos, $last- $pos); $last += 4 + $length; $pos = $last; } $serialize = substr($serialize, 0, $last).preg_replace_callback($re, $replace, substr($serialize, $last)); $o = unserialize($serialize); } } return $o; } $o = new stdClass; $o->n = $o; $o->r = &$o; $ser = serialize(remove_recursion($o)); echo serialize($o), PHP_EOL, $ser; $data = unserialize($ser); var_dump($data);
Output for 5.1.0 - 5.6.28, 7.0.0 - 7.1.0
O:8:"stdClass":2:{s:1:"n";r:1;s:1:"r";R:1;} O:8:"stdClass":2:{s:1:"n";s:12:"recursion_1";s:1:"r";s:12:"Recursion_1";}object(stdClass)#2 (2) { ["n"]=> string(12) "recursion_1" ["r"]=> string(12) "Recursion_1" }
Output for 5.0.2 - 5.0.5, hhvm-3.10.0 - 3.12.0
O:8:"stdClass":2:{s:1:"n";r:1;s:1:"r";r:1;} O:8:"stdClass":2:{s:1:"n";s:12:"recursion_1";s:1:"r";s:12:"recursion_1";}object(stdClass)#2 (2) { ["n"]=> string(12) "recursion_1" ["r"]=> string(12) "recursion_1" }
Output for 5.0.0 - 5.0.1
O:8:"stdClass":2:{s:1:"n";r:1;s:1:"r";r:1;} Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HcnNL on line 38 PHP_EOLO:8:"stdClass":2:{s:1:"n";s:12:"recursion_1";s:1:"r";s:12:"recursion_1";}object(stdClass)#2 (2) { ["n"]=> string(12) "recursion_1" ["r"]=> string(12) "recursion_1" }
Output for 4.3.10 - 4.4.9
O:8:"stdClass":2:{s:1:"n";O:8:"stdClass":0:{}s:1:"r";O:8:"stdClass":2:{s:1:"n";r:2;s:1:"r";R:3;}} O:8:"stdClass":2:{s:1:"n";O:8:"stdClass":0:{}s:1:"r";O:8:"stdClass":2:{s:1:"n";s:12:"recursion_2";s:1:"r";s:12:"Recursion_3";}}object(stdClass)(2) { ["n"]=> object(stdClass)(0) { } ["r"]=> object(stdClass)(2) { ["n"]=> string(12) "recursion_2" ["r"]=> string(12) "Recursion_3" } }
Output for 4.3.0 - 4.3.9
O:8:"stdClass":2:{s:1:"n";O:8:"stdClass":0:{}s:1:"r";O:8:"stdClass":2:{s:1:"n";O:8:"stdClass":0:{}s:1:"r";R:3;}} Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HcnNL on line 38 PHP_EOLO:8:"stdClass":2:{s:1:"n";O:8:"stdClass":0:{}s:1:"r";O:8:"stdClass":2:{s:1:"n";O:8:"stdClass":0:{}s:1:"r";s:12:"Recursion_3";}}object(stdClass)(2) { ["n"]=> object(stdClass)(0) { } ["r"]=> object(stdClass)(2) { ["n"]=> object(stdClass)(0) { } ["r"]=> string(12) "Recursion_3" } }