note 124194 added to function.var-export

From: Date: Fri, 13 Sep 2019 03:52:43 +0000
Subject: note 124194 added to function.var-export
Groups: php.notes 
Request: Send a blank email to php-notes+get-212176@lists.php.net to get a copy of this message
I improved my previous varexport(). <?php /** * PHP var_export() with short array syntax (square brackets) indented 2 spaces. * * NOTE: The only issue is when a string value has =>\n[, it will get converted to => [ * @link https://www.php.net/manual/en/function.var-export.php */ function varexport($expression, $return=FALSE) { $export = var_export($expression, TRUE); $patterns = [ "/array \(/" => '[', "/^([ ]*)\)(,?)$/m" => '$1]$2', "/=>[ ]?\n[ ]+\[/" => '=> [', "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3', ]; $export = preg_replace(array_keys($patterns), array_values($patterns), $export); if ((bool)$return) return $export; else echo $export; } $array = [ 'str' => 'Test spaces', 0 => 33, 1 => TRUE, [3,4,'d',[]], 'arr' => [ 'text with spaces' => '[Tes\'t"s": => [ => [ { spaces', ], "str2" => "Test's' } spaces", 'arr2' => [ 'text with spaces' => [ 'arr3' => [ 'text with spaces' => 'Te": "st \' => [ spaces', ], ], ], ]; varexport($array); // Result: ``` [ 'str' => 'Test spaces', 0 => 33, 1 => true, 2 => [ 0 => 3, 1 => 4, 2 => 'd', 3 => [ ], ], 'arr' => [ 'text with spaces' => '[Tes\'t"s": => [ => [ { spaces', ], 'str2' => 'Test\'s\' } spaces', 'arr2' => [ 'text with spaces' => [ 'arr3' => [ 'text with spaces' => 'Te": "st \' => [ spaces', ], ], ], ] ``` NOTE: The only issue is when a string value has =>\n[, it will get converted to => [ ---- Server IP: 208.43.231.9 Probable Submitter: 82.199.157.54 (proxied: 2001:569:703f:e500:58b0:4d4e:412d:7ad8) ---- Manual Page -- http://php.net/manual/en/function.var-export.php Edit -- https://master.php.net/note/edit/124194 Del: integrated -- https://master.php.net/note/delete/124194/integrated Del: useless -- https://master.php.net/note/delete/124194/useless Del: bad code -- https://master.php.net/note/delete/124194/bad+code Del: spam -- https://master.php.net/note/delete/124194/spam Del: non-english -- https://master.php.net/note/delete/124194/non-english Del: in docs -- https://master.php.net/note/delete/124194/in+docs Del: other reasons-- https://master.php.net/note/delete/124194 Reject -- https://master.php.net/note/reject/124194 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#212176) next »