Created
November 20, 2019 07:04
-
-
Save jamesBan/03a314cd12e98e3d7d9d73748603b80e to your computer and use it in GitHub Desktop.
var_dump 转换
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //@see https://stackoverflow.com/questions/3531857/convert-var-dump-of-array-back-to-array-variable | |
| function unvar_dump($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*bool\\(true\\)\\s*$#m', | |
| '#^\\s*bool\\(false\\)\\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', | |
| 'b:1', | |
| 'b:0', | |
| '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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment