What is the difference between explode() and str_getcsv()?

While both explode and str_getcsv do the same thing. I have hear that str_getcsv is most effective, can someone tell me which one should I use?

The manual can often be useful. Can you spot the differences?

array str_getcsv ( 
    string $input [, 
    string $delimiter = "," [, 
    string $enclosure = '"' [, 
    string $escape = "\\" ]]] )

array explode ( 
    string $delimiter , 
    string $string [, 
    int $limit = PHP_INT_MAX ] )

Of course, 99% of the time you will be reading csv from a file in which case use

array fgetcsv ( 
    resource $handle [, 
    int $length = 0 [, 
    string $delimiter = "," [, 
    string $enclosure = '"' [, 
    string $escape = "\" ]]]] )

Thanks for the explanation, but recently I studied that fdatacsv is more reliable rather than those two functions?

There is no fdatacsv function in php. Are you perhaps talking about a 3rd party library? You really need to provide a bit more info. Perhaps a link to what you are studying?

I routinely read csv files. Never had a problem with the built in functions though I suppose there could be edge cases.

No no no… by mistake I have written wrong name, it is fgetcsv :frowning:

Yes, It is better. When opening from a file. Use str_getcsv() only when you have the CSV already as a string in your program.

Fake signature removed

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.