3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * Polyfills. */ function stripslashes_from_strings_only( $value ) { return is_string( $value ) ? stripslashes( $value ) : $value; } function is_serialized( $data, $strict = true ) { // If it isn't a string, it isn't serialized. if ( ! is_string( $data ) ) { return false; } $data = trim( $data ); if ( 'N;' === $data ) { return true; } if ( strlen( $data ) < 4 ) { return false; } if ( ':' !== $data[1] ) { return false; } if ( $strict ) { $lastc = substr( $data, -1 ); if ( ';' !== $lastc && '}' !== $lastc ) { return false; } } else { $semicolon = strpos( $data, ';' ); $brace = strpos( $data, '}' ); // Either ; or } must exist. if ( false === $semicolon && false === $brace ) { return false; } // But neither must be in the first X characters. if ( false !== $semicolon && $semicolon < 3 ) { return false; } if ( false !== $brace && $brace < 4 ) { return false; } } $token = $data[0]; switch ( $token ) { case 's': if ( $strict ) { if ( '"' !== substr( $data, -2, 1 ) ) { return false; } } elseif ( false === strpos( $data, '"' ) ) { return false; } // Or else fall through. case 'a': case 'O': case 'E': return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); case 'b': case 'i': case 'd': $end = $strict ? '$' : ''; return (bool) preg_match( "/^{$token}:[0-9.E+-]+;$end/", $data ); } return false; } function map_deep( $value, $callback ) { if ( is_array( $value ) ) { foreach ( $value as $index => $item ) { $value[ $index ] = map_deep( $item, $callback ); } } elseif ( is_object( $value ) ) { $object_vars = get_object_vars( $value ); foreach ( $object_vars as $property_name => $property_value ) { $value->$property_name = map_deep( $property_value, $callback ); } } else { $value = call_user_func( $callback, $value ); } return $value; } function maybe_unserialize( $data ) { if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in. return @unserialize( trim( $data ) ); } return $data; } /* * Test. */ $test_data = 'a:2:{i:0;O:14:"SuperCustomXyz":1:{s:17:" SuperCustomXyz x";s:4:"test";}s:8:"testdata";a:2:{s:1:"a";i:1;s:1:"b";i:2;}}'; $unserialized = maybe_unserialize($test_data); $res = map_deep($unserialized, 'stripslashes_from_strings_only'); print_r($res);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
Fatal error: Uncaught Error: The script tried to modify a property on an incomplete object. Please ensure that the class definition "SuperCustomXyz" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in /in/WJiN0:78 Stack trace: #0 /in/WJiN0(73): map_deep(Object(__PHP_Incomplete_Class), 'stripslashes_fr...') #1 /in/WJiN0(100): map_deep(Array, 'stripslashes_fr...') #2 {main} thrown in /in/WJiN0 on line 78
Process exited with code 255.
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Notice: map_deep(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "SuperCustomXyz" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in /in/WJiN0 on line 78 Notice: map_deep(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "SuperCustomXyz" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in /in/WJiN0 on line 78 Array ( [0] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => SuperCustomXyz [ SuperCustomXyz x] => test ) [testdata] => Array ( [a] => 1 [b] => 2 ) )
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
Array ( [0] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => SuperCustomXyz [ SuperCustomXyz x] => test ) [testdata] => Array ( [a] => 1 [b] => 2 ) )
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9
Array ( [0] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => supercustomxyz [ SuperCustomXyz x] => test ) [testdata] => Array ( [a] => 1 [b] => 2 ) )

preferences:
92.66 ms | 412 KiB | 5 Q