3v4l.org

run code in 300+ PHP versions simultaneously
<?php class LogData { private const LOG_TYPE_MISSING_PARENT = 'MISSING_PARENT'; public array $lines = []; public function __construct(public readonly string $type, public readonly ?string $dateTime = null) { } /** * This object is used just in case the log file does not start with a log type. */ public static function createMissingParentHolder(): self { return new self(self::LOG_TYPE_MISSING_PARENT); } } $logData = <<<EOT INFO - 2023-05-23 09:10:45 --> CSRF token verified. CRITICAL - 2023-05-23 09:24:54 --> Undefined array key 158 in APPPATH/Views/tables/components/record_year__months_regions.php on line 18. 1 APPPATH/Views/tables/components/record_year__months_regions.php(18): CodeIgniter\Debug\Exceptions->errorHandler(2, 'Undefined array key 158', 'FILEPATH', 18) 17 FCPATH/index.php(67): CodeIgniter\CodeIgniter->run() CRITICAL - 2023-05-23 09:26:33 --> Undefined array key 158 in APPPATH/Views/tables/components/record_year__months_regions.php on line 18. EOT; $fp = fopen('php://memory', 'rb+'); fwrite($fp, $logData); rewind($fp); $keywords = ['INFO', 'CRITICAL', 'ERROR', 'WARNING', 'DEBUG']; $logObjects = []; $currentLogObject = null; while (($line = fgets($fp)) !== false) { $words = explode(' ', $line); $firstWord = $words[0] ?? ''; $timeWord = $words[3] ?? ''; // See if we found a special word if (in_array($firstWord, $keywords, true)) { // If we have an existing object, store it if ($currentLogObject) { $logObjects[] = $currentLogObject; } // Create a new object $currentLogObject = new LogData($firstWord, $timeWord); } elseif ($currentLogObject === null) { // We didn't find a special word, so make sure something exists to stash the line $currentLogObject = LogData::createMissingParentHolder(); } // No matter what happens above, stash the line $currentLogObject->lines[] = $line; } // At the end of the loop, make sure to store the last object if ($currentLogObject !== null) { $logObjects[] = $currentLogObject; } var_dump($logObjects);

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
161.29 ms | 1698 KiB | 15 Q