--- /dev/null
+<?php
+
+/**
+ * dolphin. Collection of useful PHP skeletons.
+ * Copyright (C) 2013 Johannes 'Banana' Keßler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program. If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+/**
+ * how to "repair" an serilized string to get the data back.
+ * it can not solve everything but it might help to recover some otherwise lost data
+ */
+
+/**
+ * the error message "Error at offset"
+ * this inticates a malformed string. with the following method you can
+ * find out where and what could be the error
+ * At first use the exact offset from the error message and choose a length of 3
+ * then you can get backwards and choose a longer range to identify the error
+ * if you have utf-8 data, make sure you set the right encoding header.
+ */
+var_export(substr($serializedStr, OFFSET, LENGTH););
+
+/**
+ * there is a better way to store and save the data to avoid malformed data
+ */
+$toDatabse = base64_encode(serialize($data)); // Save to database
+$fromDatabase = unserialize(base64_decode($data)); //Getting Save Format
+
+/**
+ * finding the error in the serilized string.
+ * based on a idea from stackoverflow
+ *
+ * @param string The serilized string
+ */
+function findSerializeError($data1) {
+ echo "<pre>";
+ $data2 = preg_replace ( '!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'",$data1 );
+ $max = (strlen ( $data1 ) > strlen ( $data2 )) ? strlen ( $data1 ) : strlen ( $data2 );
+
+ echo $data1 . PHP_EOL;
+ echo $data2 . PHP_EOL;
+
+ for($i = 0; $i < $max; $i ++) {
+
+ if (@$data1 {$i} !== @$data2 {$i}) {
+
+ echo "Diffrence ", @$data1 {$i}, " != ", @$data2 {$i}, PHP_EOL;
+ echo "\t-> ORD number ", ord ( @$data1 {$i} ), " != ", ord ( @$data2 {$i} ), PHP_EOL;
+ echo "\t-> Line Number = $i" . PHP_EOL;
+
+ $start = ($i - 20);
+ $start = ($start < 0) ? 0 : $start;
+ $length = 40;
+
+ $point = $max - $i;
+ if ($point < 20) {
+ $rlength = 1;
+ $rpoint = - $point;
+ } else {
+ $rpoint = $length - 20;
+ $rlength = 1;
+ }
+
+ echo "\t-> Section Data1 = ", substr_replace ( substr ( $data1, $start, $length ), "<b style=\"color:green\">{$data1 {$i}}</b>", $rpoint, $rlength ), PHP_EOL;
+ echo "\t-> Section Data2 = ", substr_replace ( substr ( $data2, $start, $length ), "<b style=\"color:red\">{$data2 {$i}}</b>", $rpoint, $rlength ), PHP_EOL;
+ }
+ }
+}
+
+?>
\ No newline at end of file
--- /dev/null
+<?php
+
+/**
+ * dolphin. Collection of useful PHP skeletons.
+ * Copyright (C) 2013 Johannes 'Banana' Keßler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program. If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+
+/**
+ * can return the output of var_dump() into a variable.
+ * nested and complex strutures are supported.
+ * based on an idea from stackoverflow
+ *
+ * @param string The output of var_dump()
+ */
+function var_dump_to_variable($str) {
+
+ if (strpos($str, "\n") === false) {
+ //Add new lines:
+ $regex = array(
+ '#(\\[.*?\\]=>)#',
+ '#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
+ );
+ $str = preg_replace($regex, "\n\\1", $str);
+ $str = trim($str);
+ }
+ $regex = array(
+ '#^\\040*NULL\\040*$#m',
+ '#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
+ '#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
+ '#^\\s*int\\((.*?)\\)\\s*$#m',
+ '#^\\s*float\\((.*?)\\)\\s*$#m',
+ '#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
+ '#\\s*?\\r?\\n\\s*#m',
+ );
+ $replace = array(
+ 'N',
+ 'a:\\1:{',
+ 's:\\1:\\2',
+ 'i:\\1',
+ 'd:\\1',
+ 'i:\\1',
+ ';'
+ );
+ $serialized = preg_replace($regex, $replace, $str);
+ $func = create_function(
+ '$match',
+ 'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
+ );
+ $serialized = preg_replace_callback(
+ '#\\s*\\["(.*?)"\\]\\s*=>#',
+ $func,
+ $serialized
+ );
+ $func = create_function(
+ '$match',
+ 'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
+ );
+ $serialized = preg_replace_callback(
+ '#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#',
+ $func,
+ $serialized
+ );
+ $serialized = preg_replace(
+ array('#};#', '#{;#'),
+ array('}', '{'),
+ $serialized
+ );
+
+ return unserialize($serialized);
+}
+?>
\ No newline at end of file