Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fputcsv and integer typcasting to string

So I have a line that I want to do a fputcsv on that has some integers (that I need to be treated as strings but they are numbers). These integers have leading zeroes that get cut off when I do the fputcsv but I don't want that to occur, is there any way around this? I tried just typcasting as (string) and putting my variable in quotations but the only way I have found so far is to just put quotes around the entire number which leads the quotation marks to be shown in the csv file when I open it up in excel, which I don't want to occur. Does anyone know of a way to get this to work? I think the fputcsv is just automatically assigning this variable a type for some reason and making it a integer or something...

EDIT Example Text:

What I fputcsv:

02305109

What I get in the csv file opened in excel:

2305109

but the leading zero is still there when I just use vi to open said csv file. Really strange.

like image 793
Ben Nelson Avatar asked Jul 10 '12 18:07

Ben Nelson


2 Answers

I had the same problem for long numbers which I wanted as a string. Wrap it in single quotes and make it evaluable.

'="' . $yourNumber . '"'

like image 181
user1792407 Avatar answered Oct 14 '22 20:10

user1792407


Try prepending (or appending) your leading-zero integers with a null character.

$csv[0] = 02392398."\0";
like image 5
Sander Avatar answered Oct 14 '22 20:10

Sander