12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?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);
- }
- ?>
|