Getting fputcsv to write a number as string for Excel

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Getting fputcsv to write a number as string for Excel

Post by anjanesh »

Hi

I have an $row that is an array. Im writing to a csv file which I opened in Excel. Problem is, that Excel is treating the Phone column as a number. So I did $row['Phone'] = '"'.$row['Phone'].'"';

Code: Select all

$row['Phone'] = '"'.$row['Phone'].'"';
fputcsv ($h, $row)."\n";
But now fputcsv is entering it as

Code: Select all

value5,"""56565656""",value7
so in Excel it shows up as

Code: Select all

value5,"56565656",value7
But I want is

Code: Select all

value5,"56565656",value7
so that Excel will treat it as a string and not a string with double-quotes.

How to I 'tell' fgetcsv not enter 3 quotes but only 1 ?

Thanks
Post Reply