Created
September 5, 2019 06:53
-
-
Save ajcastro/e906922f737d0aa63dfd05b29bfe2f1d to your computer and use it in GitHub Desktop.
PHP var_export() with short array syntax (square brackets) indented 4 spaces
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 | |
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; | |
} |
Author
ajcastro
commented
Sep 10, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment