3v4l.org

run code in 300+ PHP versions simultaneously
<?php function fputcsv2(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"', $escape_char = '\\') { $str = ''; foreach ($fields as $value) { $needQuote = strpos($value, $delimiter) !== false || strpos($value, $enclosure) !== false || strpos($value, "\n") !== false || strpos($value, "\r") !== false || strpos($value, "\t") !== false || strpos($value, ' ') !== false; if ($needQuote) { $str2 = $enclosure; $escaped = 0; $len = strlen($value); for ($i = 0; $i < $len; $i++) { if ($value[$i] == $escape_char) { $escaped = 1; } else if (!$escaped && $value[$i] == $enclosure) { $str2 .= $enclosure; } else { $escaped = 0; } $str2 .= $value[$i]; } $str2 .= $enclosure; $str .= $str2.$delimiter; } else { $str .= $value.$delimiter; } } $str = substr($str,0,-1); $str .= "\n"; return fwrite($handle, $str); } $list = array ( array('aaa,', 'bbb', 'ccc', 'dddd'), array('123', '456', '789'), array('"aaa"', '"bbb"') ); $fp = fopen('/tmp/file.csv', 'w'); foreach ($list as $fields) { fputcsv2($fp, $fields, ''); } fclose($fp); echo file_get_contents('/tmp/file.csv');
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
"aaa,""bbb""ccc""dddd "123""456""789 """aaa""""""bbb""
Output for 5.3.22 - 5.3.29, 5.4.12 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 Warning: strpos(): Empty needle in /in/Vm8d2 on line 7 aaa,bbbcccddd 12345678 """aaa""""""bbb""
Output for 5.2.5 - 5.2.17, 5.3.0 - 5.3.21, 5.4.0 - 5.4.11
Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter in /in/Vm8d2 on line 7 aaa,bbbcccddd 12345678 """aaa""""""bbb""
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.4
Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos(): Empty delimiter. in /in/Vm8d2 on line 7 aaa,bbbcccddd 12345678 """aaa""""""bbb""
Output for 4.3.0 - 4.3.1
Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 Warning: strpos() [http://www.php.net/function.strpos]: Empty delimiter. in /in/Vm8d2 on line 7 aaa,bbbcccddd 12345678 """aaa""""""bbb""

preferences:
307.38 ms | 403 KiB | 464 Q