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.
Result of php executing
Full code of Format var_export().php
- <?php
- class Test {
- private $foo;
- protected $bar;
- public $fighters;
- public function __construct() {
- $this->foo = 'Nice';
- $this->bar = 'Okay';
- $this->fighters = 'Got it';
- }
- }
- $Test = [
- [
- 'a => key' => 'a => value',
- 'multiline' => 'line1
- 8 => line2',
- 'quoting' => "C'mon, this might go poorly, I've seen parsing via regex b'fo",
- 'foo' => null,
- new Test,
- -1 => -.25,
- -9.22 => false,
- 'bar' => true,
- [],
- [0],
- [
- null,
- ],
- null,
- ],
- 2 => 'bar}}}',
- 's:' => 'moo',
- 'car' => 0777,
- null,
- new mysqli("localhost", "root", "", "test"),
- mysqli_connect("localhost", "root", "", "test"),
- fopen('C:\wamp64\www\test.txt', 'r'),
- getcwd(),
- 'far' => [
- 'boo',
- ],
- ];
- $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();
- #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) {
- if (!is_array($Var)) {
- if (is_resource($Var)) {
- ob_start();
- var_dump($Var);
- $v = ob_get_clean();
- $v = preg_replace('~\R~', '', $v);
- }
- return $v;
- } else {
- $wrappedArray = [];
- 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);
File Description
- Format var_export()
- PHP Code
- 28 Oct-2019
- 4.76 Kb
You can Share it:
Latest PHP Pastes