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