Bogdaan / varexport.php
This file contains 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
function varexport ( $ expression , $ return = FALSE ) |
$ export = var_export( $ expression , TRUE ); |
$ export = preg_replace(» /^([ ]*)(.*)/m «, ‘$1$1$2’ , $ export ); |
$ array = preg_split(» / \r\n | \n | \r / «, $ export ); |
$ array = preg_replace([» /\s*array\s\($/ «, » /\)(,)?$/ «, » /\s=>\s$/ «], [ NULL , ‘]$1’ , ‘ => [‘ ], $ array ); |
$ export = join( PHP_EOL , array_filter([» [ «] + $ array )); |
if (( bool ) $ return ) return $ export ; else echo $ export ; |
> |
It doesn’t work for nested arrays. Use this one:
function shorthand_var_export($expression, $return=false) < $export = var_export($expression, true); $patterns = [ "/array \(/" =>'[', "/^([ ]*)\)(,?)$/m" => '$1]$2', ]; $output = preg_replace(array_keys($patterns), array_values($patterns), $export); if ($return) < return $output; >else < echo $output; >> $myArray = array('foo' => 'bar', 'baz'); shorthand_var_export($myArray);
Footer
You can’t perform that action at this time.