Commented failing elements - 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.

Name: Commented failing elements fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Commented failing elements.php

  1. <?php
  2. class Test {
  3.     private $foo;
  4.     protected $bar;
  5.     public $fighters;
  6.  
  7.     public function __construct() {
  8.         $this->foo      = 'Nice';
  9.         $this->bar      = 'Okay';
  10.         $this->fighters = 'Got it';
  11.     }
  12. }
  13.  
  14. $Test = [
  15.     [
  16.         'a => key'  => 'a => value',
  17.         'multiline' => 'line1
  18.                         8 => line2',
  19.         'quoting'   => "C'mon, this might go poorly, I've seen parsing via regex b'fo",
  20.         'foo'       => null,
  21.         new Test,
  22.         -1          => -.25,
  23.         -9.22       => false,
  24.         'bar'       => true,
  25.         [],
  26.         [0],
  27.         [
  28.             null,
  29.         ],
  30.         null,
  31.     ],
  32.     2     => 'bar}}}',
  33.     's:'  => 'moo',
  34.     'car' => 0777,
  35.     null,
  36.         //new mysqli("localhost", "root", "", "test"),
  37.         //mysqli_connect("localhost", "root", "", "test"),
  38.         //fopen('C:\wamp64\www\test.txt', 'r'),
  39.     //getcwd(),
  40.     'far' => [
  41.         'boo',
  42.     ],
  43. ];
  44. $Test['Check1']                  = null;
  45. $Test['Check2']                  = [];
  46. $Test['Check2']['int']           = 20;
  47. $Test['Check2']['float']         = 20.35;
  48. $Test['Check2']['string']        = 'Hello World';
  49. $Test['Check2']['bolean']        = true;
  50. $Test['Check2']['array']         = [];
  51. $Test['Check2']['array']['data'] = 'Array Text';
  52. class Example {
  53.     function foo_function() {
  54.         return "Hello World! Object";
  55.     }
  56. }
  57. $var_object                         = new Example;
  58. $Test['Check2']['array']['object']  = $var_object;
  59. $Test['Check2']['array']['object2'] = $var_object->foo_function();
  60. #function get my Type of value correctly:
  61. function myGetType($var) {
  62.     if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
  63.         return "(Type of NULL)";
  64.     }
  65.  
  66.     if (is_array($var)) {
  67.         return "(array)";
  68.     }
  69.  
  70.     if (in_array($var, array("true", "false"), true)) {
  71.         return "boolean";
  72.     }
  73.  
  74.     if ((int) $var == $var && is_numeric($var)) {
  75.         return "integer" . '(' . strlen($var) . ')';
  76.     }
  77.  
  78.     if ((float) $var == $var && is_numeric($var)) {
  79.         return "float" . '(' . strlen($var) . ')';
  80.     }
  81.  
  82.     if (is_object($var)) {
  83.         return "object";
  84.     }
  85.  
  86.     if (strpos($var, 'resource') !== false AND strpos($var, 'of type ') !== false) {
  87.         return "resource";
  88.     }
  89.  
  90.     if (is_string($var)) {
  91.         return "string" . '(' . strlen($var) . ')';
  92.     }
  93.  
  94.     return "unknown";
  95. }
  96. #function to know if Exist a resource in the text:
  97. function CheckResourceType($Var) {
  98.     if (!is_array($Var)) {
  99.         if (is_resource($Var)) {
  100.             ob_start();
  101.             var_dump($Var);
  102.             $v = ob_get_clean();
  103.             $v = preg_replace('~\R~', '', $v);
  104.         }
  105.         return $v;
  106.     } else {
  107.         $wrappedArray = [];
  108.         foreach ($Var as $k => $v) {
  109.             if (is_array($v)) {
  110.                 $wrappedArray[$k] = CheckResourceType($v);
  111.             } else {
  112.                 if (is_resource($v)) {
  113.                     ob_start();
  114.                     var_dump($v);
  115.                     $v = ob_get_clean();
  116.                     $v = preg_replace('~\R~', '', $v);
  117.                 }
  118.                 $wrappedArray[$k] = $v;
  119.             }
  120.         }
  121.         return $wrappedArray;
  122.     }
  123. }
  124.  
  125. #Main function to Format:
  126. function VarExportFormat($Var) {
  127.     $textvar = '';
  128.     if (!is_array($Var)) {
  129.         $textvar       = var_export($Var, true);
  130.         $textvar       = preg_replace('~^ +~m', '$0$0', $textvar);
  131.         $typeval       = myGetType($Var);
  132.         $textvarArr[0] = $typeval . ' ' . var_export($Var, true);
  133.     } else {
  134.         //Check Point A Start
  135.         $Var     = CheckResourceType($Var);
  136.         $textvar = var_export($Var, true);
  137.         $textvar = preg_replace('~^ +~m', '$0$0', $textvar);
  138.         $textvar = preg_split("~\R~", $textvar);
  139.         $textvar = preg_replace_callback(
  140.             "~ => \K\V+(?=,)~",
  141.             function ($m) {
  142.                 return myGetType(str_replace("'", "", $m[0])) . ": {$m[0]}";
  143.             }, $textvar
  144.         );
  145.         $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvar);
  146.         //Check Point A END
  147.     }
  148.     if (!isset($textvarArr[1])) {
  149.         $textvar = PHP_EOL . $textvarArr[0];
  150.     } else {
  151.         $textvar = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
  152.     }
  153.     if (substr($textvar, -1) == '[') {
  154.         $textvar = str_replace("[", "[]", $textvar);
  155.     }
  156.     //Check Point B Start
  157.  
  158.     $textvar = preg_replace('/(\K\v+\s+)(.+\()(\v+\s+\)],)/', '$1    $2object))[],$1],', $textvar);
  159.  
  160.     //Check Point B END
  161.  
  162.     $textvar = '' . highlight_string("<?php \n#Output of Variable:\n" . $textvar . ";\n?>", true) . '';
  163.     return $textvar;
  164. }
  165.  
  166. #Call to Format function.
  167. echo VarExportFormat($Test);
File Description
  • Commented failing elements
  • PHP Code
  • 28 Oct-2019
  • 4.61 Kb
You can Share it: