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 git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
26.85 ms | 406 KiB | 5 Q