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);
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.5 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array(3) { [0]=> object(LogData)#1 (3) { ["lines"]=> array(1) { [0]=> string(52) "INFO - 2023-05-23 09:10:45 --> CSRF token verified. " } ["type"]=> string(4) "INFO" ["dateTime"]=> string(8) "09:10:45" } [1]=> object(LogData)#2 (3) { ["lines"]=> array(4) { [0]=> string(59) "CRITICAL - 2023-05-23 09:24:54 --> Undefined array key 158 " [1]=> string(79) "in APPPATH/Views/tables/components/record_year__months_regions.php on line 18. " [2]=> string(161) " 1 APPPATH/Views/tables/components/record_year__months_regions.php(18): CodeIgniter\Debug\Exceptions->errorHandler(2, 'Undefined array key 158', 'FILEPATH', 18) " [3]=> string(56) "17 FCPATH/index.php(67): CodeIgniter\CodeIgniter->run() " } ["type"]=> string(8) "CRITICAL" ["dateTime"]=> string(8) "09:24:54" } [2]=> object(LogData)#3 (3) { ["lines"]=> array(2) { [0]=> string(59) "CRITICAL - 2023-05-23 09:26:33 --> Undefined array key 158 " [1]=> string(78) "in APPPATH/Views/tables/components/record_year__months_regions.php on line 18." } ["type"]=> string(8) "CRITICAL" ["dateTime"]=> string(8) "09:26:33" } }
Output for 8.0.1 - 8.0.28
Parse error: syntax error, unexpected identifier "string", expecting variable in /in/tLdTL on line 8
Process exited with code 255.

preferences:
59.26 ms | 1075 KiB | 4 Q