3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Ajgl\Csv; class SplFileObject 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 SplTempFileObject 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); } protected function checkEscapeChar($enclosure, $escape) { check_csv_escape($enclosure, $escape); } public function fgetcsv($delimiter=",", $enclosure='"', $escape='"') { $this->checkEscapeChar($enclosure, $escape); return parent::fgetcsv($delimiter, $enclosure, $enclosure); } public function fputcsv($fields, $delimiter=',', $enclosure='"', $escape='"') { $this->checkEscapeChar($enclosure, $escape); $this->fwrite(str_putcsv_rfc($fields, $delimiter, $enclosure)); } public function setCsvControl($delimiter=",", $enclosure='"', $escape='"') { $this->checkEscapeChar($enclosure, $escape); parent::setCsvControl($delimiter, $enclosure, $enclosure); } } function fputcsv($handle, $fields, $delimiter=',', $enclosure='"', $escape='"') { check_csv_escape($enclosure, $escape); \fwrite($handle, str_putcsv_rfc($fields, $delimiter, $enclosure)); } function fgetcsv($handle, $length=0, $delimiter=",", $enclosure='"', $escape='"') { check_csv_escape($enclosure, $escape); return \fgetcsv($handle, $length, $delimiter, $enclosure, $enclosure); } function str_getcsv($input, $delimiter=",", $enclosure='"', $escape='"') { check_csv_escape($enclosure, $escape); return \str_getcsv($input, $delimiter, $enclosure, $enclosure); } function str_putcsv($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); } function check_csv_escape($enclosure, $escape) { if ($enclosure !== $escape) { trigger_error( sprintf( "The 'escape' and 'enclosure' chars must be equals. The given 'escape' char '%s' will be ignored.", $escape ), E_USER_WARNING ); } } $a = new SplTempFileObject(); $a->fputcsv(array('Hello \"World"!')); $a->rewind(); var_dump($a->fread(4096));
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Deprecated: Return type of Ajgl\Csv\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/LeSBc on line 39 Fatal error: Declaration of Ajgl\Csv\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/LeSBc on line 45
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.2 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Fatal error: Uncaught Error: Call to undefined function Ajgl\Csv\str_putcsv_rfc() in /in/LeSBc:48 Stack trace: #0 /in/LeSBc(104): Ajgl\Csv\SplTempFileObject->fputcsv(Array) #1 {main} thrown in /in/LeSBc on line 48
Process exited with code 255.
Output for 7.3.0 - 7.3.1
Fatal error: Uncaught Error: Call to undefined function str_putcsv_rfc() in /in/LeSBc:48 Stack trace: #0 /in/LeSBc(104): Ajgl\Csv\SplTempFileObject->fputcsv(Array) #1 {main} thrown in /in/LeSBc on line 48
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Call to undefined function Ajgl\Csv\str_putcsv_rfc() in /in/LeSBc on line 48
Process exited with code 255.

preferences:
137.86 ms | 411 KiB | 5 Q