Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string to an array - PHP [duplicate]

Possible Duplicate:
Load and read a csv file with php

I have the following string:

Mr,Tom,Jones,Add1,Add2,"Add3,Add3 extra",Town,County,postcode,99999,888,777

I am tring to get the string into an array that looks like the below:

Array
(
    [0] => Mr
    [1] => Tom
    [2] => Jones
    [3] => Add1
    [4] => Add2
    [5] => "Add3,Add3 extra"
    [6] => Town
    [7] => County
    [8] => Postcode
    [9] => 99999
    [10] => 888
    [11] => 777
)

if you explode the string by "," it splits array item 5 into 2 parts which I am trying to avoid. The original data comes from a CSV.

Ultimatly I am trying to import a CSV and create a new DB on the fly with the headers and file content against the correct headers.

Thanks in advance.

like image 259
makie Avatar asked Feb 20 '23 13:02

makie


1 Answers

Try fgetcsv():

fgetcsv — Gets line from file pointer and parse for CSV fields

like image 97
Peter Kiss Avatar answered Mar 05 '23 10:03

Peter Kiss