JBZoo\Utils\Ser::fix PHP Method

fix() public static method

NOTE: This error can *frequently* occur with mismatched character sets and higher-than-ASCII characters. Contributed by Theodore R. Smith of PHP Experts, Inc.
public static fix ( string $brokenSerializedData ) : string
$brokenSerializedData string
return string
    public static function fix($brokenSerializedData)
    {
        $fixdSerializedData = preg_replace_callback('!s:(\\d+):"(.*?)";!', function ($matches) {
            $snip = $matches[2];
            return 's:' . strlen($snip) . ':"' . $snip . '";';
        }, $brokenSerializedData);
        return $fixdSerializedData;
    }

Usage Example

Example #1
0
 public function testFix()
 {
     $expectedData = array('Normal', 'High-value Char: ' . chr(231) . 'a-va?');
     $brokenSerialization = 'a:2:{i:0;s:6:"Normal";i:1;s:23:"High-value Char: ▒a-va?";}';
     $expectedError = array('errno' => 8, 'errstr' => 'unserialize(): Error at offset 55 of 60 bytes');
     $reportedError = array();
     set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use(&$reportedError) {
         $reportedError = compact('errno', 'errstr');
     });
     unserialize($brokenSerialization);
     is($expectedError['errno'], $reportedError['errno']);
     // Because HHVM's unserialize() error message does not contain enough info to properly test.
     if (!defined('HHVM_VERSION')) {
         is($expectedError['errstr'], $reportedError['errstr']);
     }
     restore_error_handler();
     $fixedSerialization = Ser::fix($brokenSerialization);
     $unserializedData = unserialize($fixedSerialization);
     is($expectedData[0], $unserializedData[0], 'Did not properly fix the broken serialized data.');
     is(substr($expectedData[1], 0, 10), substr($unserializedData[1], 0, 10), 'Did not properly fix the broken serialized data.');
 }