Field separation in array

Asked

Viewed 255 times

2

I have a string consisting of (enclosed quotes):

"campo","outrocampo","maisumcampo"

I need to separate these fields, without the quotes, into an array.

What I’m doing is a

 $string = str_replace('"', NULL, $string)

and then a

 $array = explode(',', $string);

However, if any of the fields in the original string will contain a comma inside the quotes, I think I will have problems. How to avoid this?

  • 8

    Avoid headlines like "HELP NEED HELP".

  • 6

    Best title ever on SOPT :D Then I delete the comment, but had to comment.

  • 3

    @Jorgeb. at least he was sincere right :D

1 answer

4


How about using a function that was created specifically to read CSV?

$array = str_getcsv($string);

More details: Documentation

  • 1

    fgetcsv($string) is also right, only it reads every line and then interprets as file csv

Browser other questions tagged

You are not signed in. Login or sign up in order to post.