Skip to main content
Rollback to Revision 3
Source Link
Heslacher
  • 50.4k
  • 5
  • 81
  • 175

https://wtools.io/php-sandbox/borN

Updated Scripthttps://wtools.io/php-sandbox/biDk

<?php
#Class Initialization#Variable:
class
$Test Example {
    function foo_function() {
        return "Hello World! Object";
    }
}
$var_object = new Example;  = [];
#Store$Test['Check1'] Class in and Array and other Sort of variables
$Test         = null;
$Test['Check2']                  = [];
$Test['Check1']$Test['Check2']['int']           = 20;
$Test['Check2']['float']         = null;20.35;
$Test['Check2']$Test['Check2']['string']        = 'Hello World';
$Test['Check2']['bolean']        = [];true;
$Test['Check2']['int']$Test['Check2']['array']         = [];
$Test['Check2']['array']['data'] = 20;'Array Text';
$Test['Check2']['float']class Example {
    function foo_function() {
 = 20.35;
$Test['Check2']['string']      return "Hello =World! 'HelloObject";
 World';   }
$Test['Check2']['bolean']}
$var_object        = true;
$Test['Check2']['array']         = [];
$Test['Check2']['array']['data']      = 'Arraynew Text';Example;
$Test['Check2']['array']['object']  = $var_object;
$Test['Check2']['array']['object2'] = $var_object->foo_function();
#function get my
#Script Type of value correctly:
function myGetType($var) {
    if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
        return "(Type of NULL)";
    }

    if (is_array($var)) {
        return "(array)";"array";
    }

    if (in_array($var, array("true", "false"), true)) {
        return "boolean";
    }

    if ((int) $var == $var && is_numeric($var)) {
        return "integer" . '(' . strlen($var) . ')';
    }

    if ((float) $var == $var && is_numeric($var)) {
        return "float" . '(' . strlen($var) . ')';
    }

    if (is_object($var)) {
        return "object";
    }

    if (strpos($var, 'resource') !== false AND strposis_resource($var, 'of type ') !== false) {
        return "resource";
    }

    if (is_string($var)) {
        return "string" . '(' . strlen($var) . ')';
    }

    return "unknown";
}
#function to know if Exist a resource in the
#Script textAnalisis:
function CheckResourceType($Var) {
    foreach VarExportFormat($Var as $k => $v) {
     $textvar   if (is_array($v))= {'';
         $textvar   $wrappedArray[$k] = CheckResourceTypevar_export($v$Var, true);
        } else {
        $textvar    if= preg_replace(is_resource"/^($v)) {
               [ ob_start(]*);
                var_dump($v.*);
                $v/m", ='$1$1$2', ob_get_clean($textvar);
                $v$textvarArr = preg_replacepreg_split('~\R~', ''"/\r\n|\n|\r/", $v$textvar);
            }
           # $wrappedArray[$k]Analisis =del $v;tipo.
      foreach ($textvarArr }
as $key => $value) }{
    return $wrappedArray;
}

#Main function to Format:
function VarExportFormatpreg_match($Var'~=>\\s(.*?) {
,~', $value, $newvalue);
  $textvar = '';
    if (!is_arrayempty($Var$newvalue)) {
        $textvar       = var_export($Var, true);
        $textvar $newvalue[1]      = preg_replacestr_replace('~^ +~m'"'", '$0$0'"", $textvar$newvalue[1]);
        $typeval       =$typeval myGetType($Var);
        $textvarArr[0] = $typeval . ' ' . var_exportmyGetType($Var, true$newvalue[1]);
    } else {
        //Check Point A Start
        $Var     = CheckResourceType($Var);
        $textvar$value = var_export($Var, true);
        $textvar = preg_replacestr_replace('~^ +~m',"=> '$0$0'", $textvar);
    "=> " . $typeval $textvar. =': preg_split("~\R~"', $textvar$value);
        $textvar = preg_replace_callback(
            "~ =>$textvarArr[$key] \K\V+(?=,)~",
            function ($m) {$value;
                return myGetType(str_replace("'", "", $m[0])) . ": {$m[0]}";
            }, $textvar
        );
        $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvar$textvarArr);
        //Check Point A END
    }
    if (!isset($textvarArr[1])) {
        $textvar = PHP_EOL . $textvarArr[0];
    } else {
        $textvar = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
    }
    if (substr($textvar, -1) == '[') {
        $textvar = str_replace("[", "[]", $textvar);
    }
    //Check Point B Start

    $textvar = preg_replace('/(\K\v+\s+)(.+\()str_replace(\v+\s+\)],)/'"__set_state", '$1    $2object)"__set_state(object)[],$1],'", $textvar);

    //Check Point B END
    
    $textvar = highlight_string("<?php \n#Output\n#output of Variable:\n" . $textvar . ";\n?>", true);
    return $textvar;
}

#Call to Format function.
echo VarExportFormat($Test);

Solve with this:

https://regex101.com/r/pk5ICt/5

https://wtools.io/php-sandbox/borN

Updated Script

<?php
#Class Initialization
class Example {
    function foo_function() {
        return "Hello World! Object";
    }
}
$var_object = new Example;
#Store Class in and Array and other Sort of variables
$Test                            = [];
$Test['Check1']                  = null;
$Test['Check2']                  = [];
$Test['Check2']['int']           = 20;
$Test['Check2']['float']         = 20.35;
$Test['Check2']['string']        = 'Hello World';
$Test['Check2']['bolean']        = true;
$Test['Check2']['array']         = [];
$Test['Check2']['array']['data'] = 'Array Text';
$Test['Check2']['array']['object']  = $var_object;
$Test['Check2']['array']['object2'] = $var_object->foo_function();
#function get my Type of value correctly:
function myGetType($var) {
    if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
        return "(Type of NULL)";
    }

    if (is_array($var)) {
        return "(array)";
    }

    if (in_array($var, array("true", "false"), true)) {
        return "boolean";
    }

    if ((int) $var == $var && is_numeric($var)) {
        return "integer" . '(' . strlen($var) . ')';
    }

    if ((float) $var == $var && is_numeric($var)) {
        return "float" . '(' . strlen($var) . ')';
    }

    if (is_object($var)) {
        return "object";
    }

    if (strpos($var, 'resource') !== false AND strpos($var, 'of type ') !== false) {
        return "resource";
    }

    if (is_string($var)) {
        return "string" . '(' . strlen($var) . ')';
    }

    return "unknown";
}
#function to know if Exist a resource in the text:
function CheckResourceType($Var) {
    foreach ($Var as $k => $v) {
        if (is_array($v)) {
            $wrappedArray[$k] = CheckResourceType($v);
        } else {
            if (is_resource($v)) {
                ob_start();
                var_dump($v);
                $v = ob_get_clean();
                $v = preg_replace('~\R~', '', $v);
            }
            $wrappedArray[$k] = $v;
        }
    }
    return $wrappedArray;
}

#Main function to Format:
function VarExportFormat($Var) {
    $textvar = '';
    if (!is_array($Var)) {
        $textvar       = var_export($Var, true);
        $textvar       = preg_replace('~^ +~m', '$0$0', $textvar);
        $typeval       = myGetType($Var);
        $textvarArr[0] = $typeval . ' ' . var_export($Var, true);
    } else {
        //Check Point A Start
        $Var     = CheckResourceType($Var);
        $textvar = var_export($Var, true);
        $textvar = preg_replace('~^ +~m', '$0$0', $textvar);
        $textvar = preg_split("~\R~", $textvar);
        $textvar = preg_replace_callback(
            "~ => \K\V+(?=,)~",
            function ($m) {
                return myGetType(str_replace("'", "", $m[0])) . ": {$m[0]}";
            }, $textvar
        );
        $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvar);
        //Check Point A END
    }
    if (!isset($textvarArr[1])) {
        $textvar = PHP_EOL . $textvarArr[0];
    } else {
        $textvar = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
    }
    if (substr($textvar, -1) == '[') {
        $textvar = str_replace("[", "[]", $textvar);
    }
    //Check Point B Start

    $textvar = preg_replace('/(\K\v+\s+)(.+\()(\v+\s+\)],)/', '$1    $2object))[],$1],', $textvar);

    //Check Point B END
    
    $textvar = highlight_string("<?php \n#Output of Variable:\n" . $textvar . ";\n?>", true);
    return $textvar;
}

#Call to Format function.
echo VarExportFormat($Test);

Solve with this:

https://regex101.com/r/pk5ICt/5

https://wtools.io/php-sandbox/biDk

<?php
#Variable:

$Test                            = [];
$Test['Check1']                  = null;
$Test['Check2']                  = [];
$Test['Check2']['int']           = 20;
$Test['Check2']['float']         = 20.35;
$Test['Check2']['string']        = 'Hello World';
$Test['Check2']['bolean']        = true;
$Test['Check2']['array']         = [];
$Test['Check2']['array']['data'] = 'Array Text';
class Example {
    function foo_function() {
        return "Hello World! Object";
    }
}
$var_object                         = new Example;
$Test['Check2']['array']['object']  = $var_object;
$Test['Check2']['array']['object2'] = $var_object->foo_function();

#Script Type:
function myGetType($var) {
    if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
        return "(NULL)";
    }

    if (is_array($var)) {
        return "array";
    }

    if (in_array($var, array("true", "false"), true)) {
        return "boolean";
    }

    if ((int) $var == $var && is_numeric($var)) {
        return "integer" . '(' . strlen($var) . ')';
    }

    if ((float) $var == $var && is_numeric($var)) {
        return "float" . '(' . strlen($var) . ')';
    }

    if (is_object($var)) {
        return "object";
    }

    if (is_resource($var)) {
        return "resource";
    }

    if (is_string($var)) {
        return "string" . '(' . strlen($var) . ')';
    }

    return "unknown";
}

#Script Analisis:
function VarExportFormat($Var) {
    $textvar    = '';
    $textvar    = var_export($Var, true);
    $textvar    = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $textvar);
    $textvarArr = preg_split("/\r\n|\n|\r/", $textvar);
    # Analisis del tipo.
    foreach ($textvarArr as $key => $value) {
        preg_match('~=>\\s(.*?),~', $value, $newvalue);
        if (!empty($newvalue)) {
            $newvalue[1]      = str_replace("'", "", $newvalue[1]);
            $typeval          = myGetType($newvalue[1]);
            $value            = str_replace("=> ", "=> " . $typeval . ': ', $value);
            $textvarArr[$key] = $value;
        }
    }
    $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvarArr);
    $textvar    = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
    if (substr($textvar, -1) == '[') {
        $textvar = str_replace("[", "[]", $textvar);
    }
    $textvar = str_replace("__set_state", "__set_state(object)", $textvar);

    $textvar = highlight_string("<?php \n#output of Variable:\n" . $textvar . ";\n?>", true);
    return $textvar;
}

echo VarExportFormat($Test);
added 1373 characters in body
Source Link

https://wtools.io/php-sandbox/biDkhttps://wtools.io/php-sandbox/borN

Updated Script

<?php
#Variable:

$Test   #Class Initialization
class Example {
    function foo_function() {
        return "Hello World! Object";
    }
}
$var_object = [];new Example;
$Test['Check1']#Store Class in and Array and other Sort of variables
$Test         = null;
$Test['Check2']                  = [];
$Test['Check2']['int']  $Test['Check1']         = 20;
$Test['Check2']['float']         = 20.35;null;
$Test['Check2']['string']$Test['Check2']        = 'Hello World';
$Test['Check2']['bolean']        = true;[];
$Test['Check2']['array']$Test['Check2']['int']         = [];
$Test['Check2']['array']['data'] = 'Array Text';
class Example {20;
 $Test['Check2']['float']   function foo_function() {
    = 20.35;
$Test['Check2']['string']   return "Hello World! Object";
  = 'Hello }
}World';
$var_object     $Test['Check2']['bolean']        = true;
$Test['Check2']['array']         = [];
$Test['Check2']['array']['data'] = new'Array Example;Text';
$Test['Check2']['array']['object']  = $var_object;
$Test['Check2']['array']['object2'] = $var_object->foo_function();
 
#Script#function get my Type of value correctly:
function myGetType($var) {
    if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
        return "(Type of NULL)";
    }

    if (is_array($var)) {
        return "array";"(array)";
    }

    if (in_array($var, array("true", "false"), true)) {
        return "boolean";
    }

    if ((int) $var == $var && is_numeric($var)) {
        return "integer" . '(' . strlen($var) . ')';
    }

    if ((float) $var == $var && is_numeric($var)) {
        return "float" . '(' . strlen($var) . ')';
    }

    if (is_object($var)) {
        return "object";
    }

    if (is_resourcestrpos($var, 'resource') !== false AND strpos($var, 'of type ') !== false) {
        return "resource";
    }

    if (is_string($var)) {
        return "string" . '(' . strlen($var) . ')';
    }

    return "unknown";
}
 
#Script#function Analisisto know if Exist a resource in the text:
function VarExportFormatCheckResourceType($Var) {
    $textvarforeach ($Var as $k => =$v) '';{
    $textvar    =if var_export($Var,is_array($v)) true{
            $wrappedArray[$k] = CheckResourceType($v);
    $textvar    =} preg_replaceelse {
            if ("/^is_resource([$v)) ]*{
                ob_start();
                var_dump(.*$v)/m",;
 '$1$1$2', $textvar              $v = ob_get_clean();
    $textvarArr            $v = preg_splitpreg_replace("/\r\n|\n|\r/"'~\R~', $textvar'', $v);
    # Analisis del tipo.     }
    foreach ($textvarArr as $key => $value) {  $wrappedArray[$k] = $v;
        preg_match('~=>\\s(.*?),~',}
 $value, $newvalue  }
    return $wrappedArray;
}

#Main function to Format:
function VarExportFormat($Var); {
    $textvar = '';
    if (!emptyis_array($newvalue$Var)) {
        $textvar     $newvalue[1]  = var_export($Var, true);
        $textvar       = str_replacepreg_replace("'"'~^ +~m', ""'$0$0', $newvalue[1]$textvar);
        $typeval    $typeval   = myGetType($Var);
        $textvarArr[0] = myGetType$typeval . ' ' . var_export($newvalue[1]$Var, true);
    } else {
      $value  //Check Point A Start
        $Var     = str_replaceCheckResourceType("=>$Var);
 "       $textvar = var_export($Var, "=>true);
 " . $typeval . ': '  $textvar = preg_replace('~^ +~m', $value'$0$0', $textvar);
        $textvar = preg_split("~\R~", $textvar);
 $textvarArr[$key]       $textvar = $value;preg_replace_callback(
            "~ => \K\V+(?=,)~",
            function ($m) {
                return myGetType(str_replace("'", "", $m[0])) . ": {$m[0]}";
            }, $textvar
        );
        $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvarArr$textvar);
        //Check Point A END
    }
    if (!isset($textvarArr[1])) {
        $textvar = PHP_EOL . $textvarArr[0];
    } else {
        $textvar = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
    }
    if (substr($textvar, -1) == '[') {
        $textvar = str_replace("[", "[]", $textvar);
    }
    //Check Point B Start

    $textvar = str_replacepreg_replace("__set_state"'/(\K\v+\s+)(.+\()(\v+\s+\)],)/', "__set_state(object'$1    $2object)")[],$1],', $textvar);

    //Check Point B END
    
    $textvar = highlight_string("<?php \n#output\n#Output of Variable:\n" . $textvar . ";\n?>", true);
    return $textvar;
}

#Call to Format function.
echo VarExportFormat($Test);

Solve with this:

https://regex101.com/r/pk5ICt/5

https://wtools.io/php-sandbox/biDk

<?php
#Variable:

$Test                            = [];
$Test['Check1']                  = null;
$Test['Check2']                  = [];
$Test['Check2']['int']           = 20;
$Test['Check2']['float']         = 20.35;
$Test['Check2']['string']        = 'Hello World';
$Test['Check2']['bolean']        = true;
$Test['Check2']['array']         = [];
$Test['Check2']['array']['data'] = 'Array Text';
class Example {
    function foo_function() {
        return "Hello World! Object";
    }
}
$var_object                         = new Example;
$Test['Check2']['array']['object']  = $var_object;
$Test['Check2']['array']['object2'] = $var_object->foo_function();
 
#Script Type:
function myGetType($var) {
    if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
        return "(NULL)";
    }

    if (is_array($var)) {
        return "array";
    }

    if (in_array($var, array("true", "false"), true)) {
        return "boolean";
    }

    if ((int) $var == $var && is_numeric($var)) {
        return "integer" . '(' . strlen($var) . ')';
    }

    if ((float) $var == $var && is_numeric($var)) {
        return "float" . '(' . strlen($var) . ')';
    }

    if (is_object($var)) {
        return "object";
    }

    if (is_resource($var)) {
        return "resource";
    }

    if (is_string($var)) {
        return "string" . '(' . strlen($var) . ')';
    }

    return "unknown";
}
 
#Script Analisis:
function VarExportFormat($Var) {
    $textvar    = '';
    $textvar    = var_export($Var, true);
    $textvar    = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $textvar);
    $textvarArr = preg_split("/\r\n|\n|\r/", $textvar);
    # Analisis del tipo.
    foreach ($textvarArr as $key => $value) {
        preg_match('~=>\\s(.*?),~', $value, $newvalue);
        if (!empty($newvalue)) {
            $newvalue[1]      = str_replace("'", "", $newvalue[1]);
            $typeval          = myGetType($newvalue[1]);
            $value            = str_replace("=> ", "=> " . $typeval . ': ', $value);
            $textvarArr[$key] = $value;
        }
    }
    $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvarArr);
    $textvar    = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
    if (substr($textvar, -1) == '[') {
        $textvar = str_replace("[", "[]", $textvar);
    }
    $textvar = str_replace("__set_state", "__set_state(object)", $textvar);

    $textvar = highlight_string("<?php \n#output of Variable:\n" . $textvar . ";\n?>", true);
    return $textvar;
}

echo VarExportFormat($Test);

https://wtools.io/php-sandbox/borN

Updated Script

<?php
#Class Initialization
class Example {
    function foo_function() {
        return "Hello World! Object";
    }
}
$var_object = new Example;
#Store Class in and Array and other Sort of variables
$Test                            = [];
$Test['Check1']                  = null;
$Test['Check2']                  = [];
$Test['Check2']['int']           = 20;
$Test['Check2']['float']         = 20.35;
$Test['Check2']['string']        = 'Hello World';
$Test['Check2']['bolean']        = true;
$Test['Check2']['array']         = [];
$Test['Check2']['array']['data'] = 'Array Text';
$Test['Check2']['array']['object']  = $var_object;
$Test['Check2']['array']['object2'] = $var_object->foo_function();
#function get my Type of value correctly:
function myGetType($var) {
    if (is_null($var) OR $var == 'null' OR $var == 'NULL') {
        return "(Type of NULL)";
    }

    if (is_array($var)) {
        return "(array)";
    }

    if (in_array($var, array("true", "false"), true)) {
        return "boolean";
    }

    if ((int) $var == $var && is_numeric($var)) {
        return "integer" . '(' . strlen($var) . ')';
    }

    if ((float) $var == $var && is_numeric($var)) {
        return "float" . '(' . strlen($var) . ')';
    }

    if (is_object($var)) {
        return "object";
    }

    if (strpos($var, 'resource') !== false AND strpos($var, 'of type ') !== false) {
        return "resource";
    }

    if (is_string($var)) {
        return "string" . '(' . strlen($var) . ')';
    }

    return "unknown";
}
#function to know if Exist a resource in the text:
function CheckResourceType($Var) {
    foreach ($Var as $k => $v) {
        if (is_array($v)) {
            $wrappedArray[$k] = CheckResourceType($v);
        } else {
            if (is_resource($v)) {
                ob_start();
                var_dump($v);
                $v = ob_get_clean();
                $v = preg_replace('~\R~', '', $v);
            }
            $wrappedArray[$k] = $v;
        }
    }
    return $wrappedArray;
}

#Main function to Format:
function VarExportFormat($Var) {
    $textvar = '';
    if (!is_array($Var)) {
        $textvar       = var_export($Var, true);
        $textvar       = preg_replace('~^ +~m', '$0$0', $textvar);
        $typeval       = myGetType($Var);
        $textvarArr[0] = $typeval . ' ' . var_export($Var, true);
    } else {
        //Check Point A Start
        $Var     = CheckResourceType($Var);
        $textvar = var_export($Var, true);
        $textvar = preg_replace('~^ +~m', '$0$0', $textvar);
        $textvar = preg_split("~\R~", $textvar);
        $textvar = preg_replace_callback(
            "~ => \K\V+(?=,)~",
            function ($m) {
                return myGetType(str_replace("'", "", $m[0])) . ": {$m[0]}";
            }, $textvar
        );
        $textvarArr = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => array ['], $textvar);
        //Check Point A END
    }
    if (!isset($textvarArr[1])) {
        $textvar = PHP_EOL . $textvarArr[0];
    } else {
        $textvar = join(PHP_EOL, array_filter(["array ["] + $textvarArr));
    }
    if (substr($textvar, -1) == '[') {
        $textvar = str_replace("[", "[]", $textvar);
    }
    //Check Point B Start

    $textvar = preg_replace('/(\K\v+\s+)(.+\()(\v+\s+\)],)/', '$1    $2object))[],$1],', $textvar);

    //Check Point B END
    
    $textvar = highlight_string("<?php \n#Output of Variable:\n" . $textvar . ";\n?>", true);
    return $textvar;
}

#Call to Format function.
echo VarExportFormat($Test);

Solve with this:

https://regex101.com/r/pk5ICt/5

Tweeted twitter.com/StackCodeReview/status/1180407371719434241
Typo in my title edit
Link
mickmackusa
  • 8.7k
  • 1
  • 16
  • 31

Parsing and formatting var_export()'s rreturned string

Some improvements to English, added tag
Source Link
mickmackusa
  • 8.7k
  • 1
  • 16
  • 31
Loading
Source Link
Loading