How to use str_getcsv () and ignore commas between quotes?

Here is my current code.

<?php$da_data = $_POST['dadata'];
$da_data = htmlspecialchars($da_data, ENT_QUOTES);
$da_data = str_replace('&lt;', '', $da_data);
$da_data = str_replace("&gt;", '', $da_data);
$da_data = str_getcsv($da_data,  ",", "'");
print_r($da_data);
?>

Sample data:

"Bill, Rose Mary" <bill@co.bill.ca.us>,"asasd, test" <test@co.test.ca.us>,

he spits out

Array (
[0] => \"Bill
[1] => Rose Mary\" bill@co.bill.ca.us
[2] => \"asasd
[3] => test\" test@co.test.ca.us
[4] =>
)

I would like the name and email address to be combined against each other. What am I missing?

+4
source share
1 answer
$da_data = str_getcsv($da_data,  ",", "'");
//                                     ^

will read this as you want:

'Bill, Rose Mary' <bill@co.bill.ca.us>,'asasd, test' <test@co.test.ca.us>,
^               ^                      ^           ^

But you do not use single quotes in the CSV file, as indicated in your call str_getcsv. This is ":

$da_data = str_getcsv($da_data,  ',', '"');
//                                     ^

var_dump($da_data);

Output:

array(3) {
  [0]=>
  string(36) "Bill, Rose Mary <bill@co.bill.ca.us>"
  [1]=>
  string(32) "asasd, test <test@co.test.ca.us>"
  [2]=>
  string(0) ""
}

Demo

Please note that it deletes yours ", as it is actually assumed that it contains the entire string.

In a completely different note, to make sure you get the data you need, you must convert your CSV file to the following:

"Bill, Rose Mary <bill@co.bill.ca.us>","asasd, test <test@co.test.ca.us>",
^                                    ^ ^                                ^
+4

Source: https://habr.com/ru/post/1532885/


All Articles