Solved

PHP CSV output and problem with date and escape chararacters

Posted on 2008-06-12
3
726 Views
Last Modified: 2008-06-17
I'm having a problem with escaping commas inside a date field when writing to a CSV file,

the date is in the format of "June 12, 2008, 1:29 pm"
the commas wreck the formatting, how would i change it so this doesn't happen?

Cheers
$list = array (
    'Name,County,Date completed,Coach name, Level, How long known,Psych name,MTP overall score,Statement 1,statement 2,statement 3,Fight average,ID average,CMC average,Res Average,Fight statement 1,Fight Statemt 2,Fight Statemt 3,ID statemt 1,ID statement 2,ID statement 3,CMC statemt 1,CMC statemt 2,CMC Statemt 3,Res Statemt 1,Res Statemt 2,Res Statemt 3',
    ''.$name.','.$date.','.$temp.','.$coach.','.$namelevel.','.$coachingtime.','.$psychologist.','.$temp.','.$temp.','.$temp.','.$temp.','.$a4.','.$b4.','.$c4.','.$d4.','.$a1.','.$a2.','.$a3.','.$b1.','.$b2.','.$b3.','.$c1.','.$c2.','.$c3.','.$d1.','.$d2.','.$d3.'');
 
 
$number = rand(0,101101);
$username = $_SESSION['Username'];
$_SESSION['csv']="temp/mycsv".$number.$username.".jpg";
$fp = fopen('temp/mycsv'.$number.$username.'.csv', 'w');
 
foreach ($list as $line) {
    fputcsv($fp, split(',', $line));
}
 
fclose($fp);

Open in new window

0
Comment
Question by:FurnakTye
[X]
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
  • 2
3 Comments
 
LVL 9

Expert Comment

by:zemond
ID: 21769328
you will need to replace the commas with another delimiter, perhaps - to produce June 12-2008-1:29 pm
0
 
LVL 9

Accepted Solution

by:
zemond earned 125 total points
ID: 21769369
$date = str_replace(',', '-', $date);
0
 
LVL 13

Expert Comment

by:marchent
ID: 21769527
You can use preg_replace()  function too, to replace comma,
$date = preg_replace("/,/", '-', $date);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
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 dynamically set the form action using jQuery.

696 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