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 "(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 (is_resource($var)) {
  49.                 return "resource";
  50.         }
  51.  
  52.         if (is_string($var)) {
  53.                 return "string" . '(' . strlen($var) . ')';
  54.         }
  55.  
  56.         return "unknown";
  57. }
  58.  
  59. #Script Analisis:
  60. function VarExportFormat($Var) {
  61.         $textvar    = '';
  62.         $textvar    = var_export($Var, true);
  63.         $textvar    = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $textvar);
  64.         $textvarArr = preg_split("/\r\n|\n|\r/", $textvar);
  65.         # Analisis del tipo.
  66.         foreach ($textvarArr as $key => $value) {
  67.                 preg_match('~=>\\s(.*?),~', $value, $newvalue);
  68.                 if (!empty($newvalue)) {
  69.                         $newvalue[1]      = str_replace("'", "", $newvalue[1]);
  70.                         $typeval          = myGetType($newvalue[1]);
  71.                         $value            = str_replace("=> ", "=> " . $typeval . ': ', $value);
  72.                         $textvarArr[$key] = $value;
  73.                 }
  74.         }
  75.         $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvarArr);
  76.         $textvar    = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
  77.         if (substr($textvar, -1) == '[') {
  78.                 $textvar = str_replace("[", "[]", $textvar);
  79.         }
  80.         $textvar = str_replace("__set_state", "__set_state(object)", $textvar);
  81.  
  82.         $textvar = highlight_string("<?php \n#output of Variable:\n" . $textvar . ";\n?>", true);
  83.         return $textvar;
  84. }
  85.  
  86. echo VarExportFormat($Test);
File Description
  • Format var_export()
  • PHP Code
  • 04 Oct-2019
  • 2.39 Kb
You can Share it: