Format var_export() - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Format var_export().php

  1. <?php
  2. #Variable:
  3.  
  4. $Test                            = [];
  5. $Test['Check1']                  = null;
  6. $Test['Check2']                  = [];
  7. $Test['Check2']['int']           = 20;
  8. $Test['Check2']['float']         = 20.35;
  9. $Test['Check2']['string']        = 'Hello (World)';
  10. $Test['Check2']['bolean']        = true;
  11. $Test['Check2']['array']         = [];
  12. $Test['Check2']['array']['data'] = 'Array Text';
  13. class Example {
  14.         function foo_function() {
  15.                 return "Hello World! Object";
  16.         }
  17. }
  18. $var_object                         = new Example;
  19. $Test['Check2']['array']['object']  = $var_object;
  20. $Test['Check2']['array']['object2'] = $var_object->foo_function();
  21.  
  22. #Script Type:
  23. function myGetType($var) {
  24.         if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
  25.                 return "(type NULL)";
  26.         }
  27.  
  28.         if (is_array($var)) {
  29.                 return "(array)";
  30.         }
  31.  
  32.         if (in_array($var, array("true", "false"), true)) {
  33.                 return "boolean";
  34.         }
  35.  
  36.         if ((int) $var == $var && is_numeric($var)) {
  37.                 return "integer" . '(' . strlen($var) . ')';
  38.         }
  39.  
  40.         if ((float) $var == $var && is_numeric($var)) {
  41.                 return "float" . '(' . strlen($var) . ')';
  42.         }
  43.  
  44.         if (is_object($var)) {
  45.                 return "object";
  46.         }
  47.  
  48.         if (strpos($var, 'resource') !== false AND strpos($var, 'of type ') !== false) {
  49.                 return "resource";
  50.         }
  51.  
  52.         if (is_string($var)) {
  53.                 return "string" . '(' . strlen($var) . ')';
  54.         }
  55.  
  56.         return "unknown";
  57. }
  58.  
  59. function CheckResourceType($Var) {
  60.         foreach ($Var as $k => $v) {
  61.                 if (is_array($v)) {
  62.                         $wrappedArray[$k] = CheckResourceType($v);
  63.                 } else {
  64.                         if (is_resource($v)) {
  65.                                 ob_start();
  66.                                 var_dump($v);
  67.                                 $v = ob_get_clean();
  68.                                 $v = preg_replace('~\R~', '', $v);
  69.                         }
  70.                         $wrappedArray[$k] = $v;
  71.                 }
  72.         }
  73.         return $wrappedArray;
  74. }
  75.  
  76. #Script Analisis:
  77. function VarExportFormat($Var) {
  78.         $textvar = '';
  79.         if (!is_array($Var)) {
  80.                 $textvar       = var_export($Var, true);
  81.                 $textvar       = preg_replace('~^ +~m', '$0$0', $textvar);
  82.                 $typeval       = myGetType($Var);
  83.                 $textvarArr[0] = $typeval . ' ' . var_export($Var, true);
  84.         } else {
  85.                 $Var     = CheckResourceType($Var);
  86.                 $textvar = var_export($Var, true);
  87.                 $textvar = preg_replace('~^ +~m', '$0$0', $textvar);
  88.                 $textvar = preg_split("~\R~", $textvar);
  89.                 # Analisis del tipo.
  90.                 $textvar = preg_replace_callback(
  91.                         "~ => \K\V+(?=,)~",
  92.                         function ($m) {
  93.                                 return myGetType(str_replace("'", "", $m[0])) . ": {$m[0]}";
  94.                         }, $textvar
  95.                 );
  96.                 $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvar);
  97.  
  98.         }
  99.         if (!isset($textvarArr[1])) {
  100.                 $textvar = PHP_EOL . $textvarArr[0];
  101.         } else {
  102.                 $textvar = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
  103.         }
  104.         if (substr($textvar, -1) == '[') {
  105.                 $textvar = str_replace("[", "[]", $textvar);
  106.         }
  107.         $textvar = str_replace("__set_state", "__set_state(object)", $textvar);
  108.         $textvar = highlight_string("<?php \n#Output of Variable:\n" . $textvar . ";\n?>", true);
  109.         return $textvar;
  110. }
  111. echo VarExportFormat($Test);
  112.  
File Description
  • Format var_export()
  • PHP Code
  • 18 Oct-2019
  • 2.84 Kb
You can Share it: