Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help with fputcsv

Posted on 2008-10-24
4
570 Views
Last Modified: 2010-04-21
I'm exporting an array to a csv file, but it isn't formatting correctly,

This is the array:

Array ( [0] => Array ( [id] => 41 [item] => HY87MB [cost] => $43,679 ) [1] => Array ( [id] => 39 [item] => UYITHO [cost] => $79,202.10 ) )

And the csv file looks like this:
41,HY87MB,$43,679
39,UYITHO,$9,202.10

When, I want it to look like this:
41,"HY87MB","$43,679"
39,"UYITHO","$9,202.10"
/* HERE IS THE CODE I'M USING */
 
$fp = fopen(testfile.csv', 'w');
foreach($result as $line) {
                foreach($line as $line2) {
                                fputcsv($fp, split(',',$line2));
                }
}
fclose($fp);
header('Location: testfile.csv');

Open in new window

0
Comment
Question by:n00b0101
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22797527
have you tried forcing the additional arguments?

fputcsv($fp, split(',',$line2), ",", "\"");
0
 

Author Comment

by:n00b0101
ID: 22797702
Yes, I did try that, but I got the same result.
0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 500 total points
ID: 22798488
as a temporary workaround, try this class. I just wrote it and have not tested it.
<?php
 
 
class putcsv {
	public $fp = NULL;
	public $row = array();
 
	public function putCSV($row) {
		$this->row = $row;
 
		$data = array();
 
		foreach ($row as $k => $v) {
			$data[] = $this->formatCSVCell($v);
		}
 
		fwrite($this->fp, implode(",", $data) . "\n");
	}
 
	private function formatCSVCell($data) {
		$useQuotes = false;
 
		$quotable = array (
				"\"" => "\"\"",
				"," => ",",
				"\n" => "\n",
				"$", "$"
			);
 
		foreach ($quotable as $char => $repl) {
			if (eregi($char, $data)) {
				$useQuotes = true;
			} else {}
		}
 
		if ($useQuotes == true) {
			foreach ($quotable as $char => $repl) {
				$data = str_replace($char, $repl, $data);
			}
 
			$data = "\"" . $data . "\"";
		}
		else {
 
		}
 
		return $data;
	}
}
 
$putcsv = new putcsv();
$putcsv->fp = fopen("file.csv", "w+");
 
foreach ($lines as $line) {
	$putcsv->putCSV($line);
}
fclose($putcsv->fp);
?>

Open in new window

0
 

Author Closing Comment

by:n00b0101
ID: 31509689
thanks for this
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question