Skip to content

Instantly share code, notes, and snippets.

View dobrydev's full-sized avatar
🏠
Working from home

Dobry Travieso dobrydev

🏠
Working from home
View GitHub Profile
@Bogdaan
Bogdaan / varexport.php
Created September 10, 2018 12:31
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));
if ((bool)$return) return $export; else echo $export;
}