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