Voting

: max(seven, seven)?
(Example: nine)

The Note You're Voting On

steven at nevvix dot com
7 years ago
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 `=> [`

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /