Skip to content

Instantly share code, notes, and snippets.

@JonatanFlores
Last active May 6, 2019 18:00
Show Gist options
  • Save JonatanFlores/a2f471da8473eaf001cc4a64d88a9e0d to your computer and use it in GitHub Desktop.
Save JonatanFlores/a2f471da8473eaf001cc4a64d88a9e0d to your computer and use it in GitHub Desktop.
PHP var_export() with short array syntax (square brackets) indented 4 spaces
<?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));
$export = preg_replace('(\d+\s=>\s)', '', $export); // REMOVE THE NUMERIC INDEXES
if ((bool)$return) return $export; else echo $export;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment