3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CsvFileObject extends \SplFileObject { use CsvFileObjectTrait; public function __construct($filename, $open_mode='r', $use_include_path=false, $context=null) { parent::__construct($filename, $open_mode, $use_include_path, $context); $this->fixCsvControl(); } } class CsvTempFileObject extends \SplTempFileObject { use CsvFileObjectTrait; public function __construct ($max_memory=null) { parent::__construct($max_memory); $this->fixCsvControl(); } } trait CsvFileObjectTrait { protected function fixCsvControl() { list($delimiter, $enclosure) = $this->getCsvControl(); $this->setCsvControl($delimiter, $enclosure); } public function fgetcsv($delimiter=",", $enclosure='"', $escape='"') { return parent::fgetcsv($delimiter, $enclosure, $enclosure, $escape='"'); } public function fputcsv($fields, $delimiter=',', $enclosure='"', $escape='"') { $this->fwrite(str_putcsv_rfc($fields, $delimiter, $enclosure)); } public function setCsvControl($delimiter=",", $enclosure='"', $escape='"') { parent::setCsvControl($delimiter, $enclosure, $enclosure); } } function fputcsv_rfc($handle, $fields, $delimiter=',', $enclosure='"') { fwrite($handle, str_putcsv_rfc($fields, $delimiter, $enclosure)); } function fgetcsv_rfc($handle, $length=0, $delimiter=",", $enclosure='"') { return fgetcsv($handle, $length, $delimiter, $enclosure, $enclosure); } function str_getcsv_rfc($input, $delimiter=",", $enclosure='"') { return str_getcsv ($input, $delimiter, $enclosure, $enclosure); } function str_putcsv_rfc($fields, $delimiter=",", $enclosure='"') { $file = new SplTempFileObject(); $file->fputcsv($fields, $delimiter, $enclosure); $file->rewind(); $line = ''; while (!$file->eof()) { $line .= $file->fgets(); } return str_replace('\\'.$enclosure, '\\'.$enclosure.$enclosure, $line); } $a = new CsvTempFileObject(); $a->fwrite('"Hello \""World""!"'."\n"); $a->rewind(); $a->setFlags(CsvTempFileObject::READ_CSV); foreach ($a as $fields) { var_dump($fields); }
Output for 8.5.0 - 8.5.1
Deprecated: Return type of CsvFileObject::fgetcsv($delimiter = ',', $enclosure = '"', $escape = '"') should either be compatible with SplFileObject::fgetcsv(string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/k2o3i on line 33 Fatal error: Declaration of CsvFileObject::fputcsv($fields, $delimiter = ',', $enclosure = '"', $escape = '"') must be compatible with SplFileObject::fputcsv(array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int|false in /in/k2o3i on line 38 Stack trace: #0 {main}
Process exited with code 255.
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14
Deprecated: Return type of CsvFileObjectTrait::fgetcsv($delimiter = ',', $enclosure = '"', $escape = '"') should either be compatible with SplFileObject::fgetcsv(string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/k2o3i on line 33 Fatal error: Declaration of CsvFileObjectTrait::fputcsv($fields, $delimiter = ',', $enclosure = '"', $escape = '"') must be compatible with SplFileObject::fputcsv(array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int|false in /in/k2o3i on line 38
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38, 7.0.0 - 7.0.31, 7.1.0 - 7.1.23, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
array(1) { [0]=> string(15) "Hello \"World"!" }

preferences:
69.7 ms | 413 KiB | 5 Q