tonelm54
asked on
Splitting a string with or without quotes
Ive been looking for a way to split a string by , unless its in "
I tried using PHP CSV split but it really didnt work very well.
Im happy to split the file up into lines to read in, but ideally Id like another regex to split the file into lines and take into account multiple lines in the CSV file if the text is inclosed in " .
Im sure Im not the first to try and read in CSV files, anyone got any advise for me?
28, 26, Project Manager, "$1,234,108"So this should return,
28
26
Project Manager
$1,234,108
Ive found a few articles which suggest the regex command ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)" but their results are weird.I tried using PHP CSV split but it really didnt work very well.
Im happy to split the file up into lines to read in, but ideally Id like another regex to split the file into lines and take into account multiple lines in the CSV file if the text is inclosed in " .
Im sure Im not the first to try and read in CSV files, anyone got any advise for me?
ASKER
Using the str_getcsv doesnt work well on all lines unfortantly, eg:-
Any suggestions????
1711072825814474786,Walsall,Incident,346456456,Request to Restore,User6,"After a quick look, found the folder in ""D:\Office\Quotations\"" Emailed user to confirm, if correct Ill move it on the server",0.25,No,2020-06-15 00:00:00 +0100,2020-06-15 11:56:53 +0100,Service Request,S Move / Change,Juan,Company6
Which splits it as:-Array
(
[0] => 1711072825814474786
[1] => Walsall
[2] => Incident
[3] => 346456456
[4] => Request to Restore
[5] => User6
[6] => After a quick look, found the folder in ""D:\Office\Quotations\"" Emailed user to confirm
[7] => if correct Ill move it on the server"
[8] => 0.25
[9] => No
[10] => 2020-06-15 00:00:00 +0100
[11] => 2020-06-15 11:56:53 +0100
[12] => Service Request
[13] => S Move / Change
[14] => Juan
[15] => Company6
)
I can see where it split with the , in the middle of the line.Any suggestions????
How about replacing any "" with a '?
$input = '1711072825814474786,Walsall,Incident,346456456,Request to Restore,User6,"After a quick look, found the folder in ""D:\Office\Quotations\"" Emailed user to confirm, if correct Ill move it on the server",0.25,No,2020-06-15 00:00:00 +0100,2020-06-15 11:56:53 +0100,Service Request,S Move / Change,Juan,Company6';
$output = str_getcsv(str_replace('""',"'", $input), $separator = "," , $enclosure = "\"");
print_r($output);
Array
(
[0] => 1711072825814474786
[1] => Walsall
[2] => Incident
[3] => 346456456
[4] => Request to Restore
[5] => User6
[6] => After a quick look, found the folder in \'D:\Office\Quotations\\' Emailed user to confirm, if correct Ill move it on the server
[7] => 0.25
[8] => No
[9] => 2020-06-15 00:00:00 +0100
[10] => 2020-06-15 11:56:53 +0100
[11] => Service Request
[12] => S Move / Change
[13] => Juan
[14] => Company6
)
You just need the extra escape parameter for the function defined as follows:
If you don't define it, it defaults to backslash.
$output = str_getcsv($input, $separator = "," , $enclosure = "\"", $escape = "\"");
I tested that and it seemed to work fine.If you don't define it, it defaults to backslash.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window