My code generates a txt file using PHP's fputcsv function.
For the delimiter, I am trying to use '|'
$query = mysql_query("SELECT email, emailSource FROM session WHERE is_complete='1' ORDER by sessionid ASC")
$filename= 'here.txt';
$fp = fopen( $filename,'w');
fputcsv($fp, array('Email address', 'Email Source'));
if(mysql_numrows($query) > 0) {
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
fputcsv($fp, array_values($row));
}
}
fclose($fp);
$contents = file_get_contents($filename);
$contents = str_replace(",", "|", $contents);
file_put_contents($filename, $contents);
The result I get is all on one line instead of showing the values on a seperate line and I also have "" around the headers.
"Email address"|"Email Source"|blah@blah.com|hi|
instead of this:
Email address|Email Source|
blah@blah.com|hi|
Please can someone tell me what I am doing wrong. Is it because I am using fputcsv and saving to a txt file?
Get rid of the str_replace / file_get_contents/ file_put_contents block. Instead of fputcsv($fp, array('...'))
, use fputcsv($fp, array('...'), '|');