简体   繁体   中英

How to insert comma in comma separated file

I am creating a comma separated file using a data array in a loop. But the problem is that if any location in the array already has a comma then it does not create a correct comma separated file.

How do I include a string with a comma in comma separated file.

Thanks

Put qoutation marks ( " ) around the string. If you also have " in the string, escape those by doubling. Example row:

"hello world, I'll call you ""Earth""", 4, 2, "another string"

So, if you have a string in the variable $field, normalize the field in this fashion:

$field = '"'. str_replace('"', '""', $field) .'"';

The usual way is to quote the field:

id,name,position
1,"Maxwell, Sam",CTO
2,Bob,Techie

This depends on what program will be reading the file.

If the parser is written as a state machine, commas should not be handled as delimiters when the string has quotes around it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM