Fputcsv and integer typcasting for string

So, I have a string in which I want to make fputcsv that has integers (which I need to treat as strings, but they are numbers). These integers have leading zeros that are truncated when I do fputcsv, but I don't want this to happen, is there any way around this? I tried just typcasting as (string) and put my variable in quotes, but the only way I have found so far is to simply put quotes around the whole number, which leads to showing quotes in the csv file when I open excel, which I do not want. Does anyone know how to make this work? I think fputcsv just automatically assigns this type variable for some reason and makes it integer or something ...

EDIT example text:

What do I need fputcsv:

02305109 

What I get in a csv file opened in excel:

 2305109 

but leading zero still exists when I just use vi to open the specified csv file. Really strange.

+8
php excel csv fputcsv
source share
8 answers

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

'= "'. $ yourNumber. '"'

+11
source share

Excel interprets the value as a number and formats it as follows. This has nothing to do with php.

There is information in this SU post: https://superuser.com/questions/234997/how-can-i-stop-excel-from-eating-my-delicious-csv-files-and-excreting-useless-da

+2
source share

Try adding (or adding) integers with a null character.

 $csv[0] = 02392398."\0"; 
+2
source share

displays it as a formula, since:

$ line ["phone"] = "= \" ". $ line [" phone "]." \ "";

0
source share

Adding one quote to the beginning of the data will solve the problem.

ie '930838493828584738 instead of 93083849382858474738, which converts to this 934E + n

If the csv file is provided by a third party, this may be a problem.

0
source share

Try the leading apostrophe (single quote). IIRC, which retains leading zeros but does not appear in Excel.

-one
source share

format the column using "00000000"

This has the advantage that it will retain the format when saving

-one
source share

You just need to add a quote to the beginning of the number:

 '123456 

and excel will not format this cell as a number.

-one
source share

All Articles