Created
February 3, 2015 13:17
-
-
Save tuki0918/0a3f64c1451f705ff912 to your computer and use it in GitHub Desktop.
fputcsv double quote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('error_reporting', E_ALL); | |
ini_set('display_errors', 1); | |
function fputcsv2($fp, $fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) { | |
$delimiter_esc = preg_quote($delimiter, '/'); | |
$enclosure_esc = preg_quote($enclosure, '/'); | |
$output = array(); | |
foreach ($fields as $field) { | |
if ($field === null && $mysql_null) { | |
$output[] = 'NULL'; | |
continue; | |
} | |
// original | |
// $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? ( | |
// $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure | |
// ) : $field; | |
$output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? ( | |
$enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure | |
) : "\"{$field}\""; | |
} | |
fwrite($fp, join($delimiter, $output) . "\n"); | |
} | |
$array = array("Windows", "Mac", "Linux", '<table style="sdc">あいうえお | |
</table>'); | |
$fp = fopen("./test.csv", "w"); | |
fputcsv2($fp, $array); | |
fclose($fp); |
$value = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? (
str_replace($enclosure, $enclosure . $enclosure, $field)
) : $field;
$output[] = $enclosure . $value . $enclosure;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://php.net/manual/ja/function.fputcsv.php