php - How to format var_export to php5.4 array syntax -
there lots of questions , answers around subject of valid php syntax var outputs, looking quick , clean way of getting output of var_export utilize valid php5.4 array syntax.
given
$arr = [ 'key' => 'value', 'mushroom' => [ 'badger' => 1 ] ]; var_export($arr);
outputs
array ( 'key' => 'value', 'mushroom' => array ( 'badger' => 1, ), ) is there quick , easy way have output array defined, using square bracket syntax?
[ 'key' => 'value', 'mushroom' => [ 'badger' => 1 ] ] is general consensus utilize regex parsing? if so, has come across decent regular expression? value level contents of arrays utilize scalar , array, no objects or classes.
i had similar laying around.
function var_export54($var, $indent="") { switch (gettype($var)) { case "string": homecoming '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"'; case "array": $indexed = array_keys($var) === range(0, count($var) - 1); $r = []; foreach ($var $key => $value) { $r[] = "$indent " . ($indexed ? "" : var_export54($key) . " => ") . var_export54($value, "$indent "); } homecoming "[\n" . implode(",\n", $r) . "\n" . $indent . "]"; case "boolean": homecoming $var ? "true" : "false"; default: homecoming var_export($var, true); } } it's not overly pretty, maybe sufficient case.
any specified types handled regular var_export. single-quoted strings, comment out string case.
php arrays php-5.4
No comments:
Post a Comment