如何使用str_getcsv()并在引号之间忽略逗号?

时间:2021-09-09 21:42:11

Here's 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);
?>

Example data:

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

it's spitting out

它吐出来了

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

I'd like to have the name and email together opposed to separated. What am I missing?

我想把名字和电子邮件放在一起反对分开。我错过了什么?

1 个解决方案

#1


4  

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

would read this like you want:

会像你想要的那样阅读:

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

But you don't use single quotes in your CSV file like you specified in your str_getcsv call. It's ":

但是,您不要像在str_getcsv调用中指定的那样在CSV文件中使用单引号。它的 ”:

$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 do note that it removes your " as they're actually supposed to be enclosing the entire string.

请注意它删除了“因为它们实际上应该包含整个字符串。

On a completely different note, to make sure that you get the right data you should transform your CSV file into the following:

在完全不同的说明中,为了确保您获得正确的数据,您应该将CSV文件转换为以下内容:

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

#1


4  

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

would read this like you want:

会像你想要的那样阅读:

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

But you don't use single quotes in your CSV file like you specified in your str_getcsv call. It's ":

但是,您不要像在str_getcsv调用中指定的那样在CSV文件中使用单引号。它的 ”:

$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 do note that it removes your " as they're actually supposed to be enclosing the entire string.

请注意它删除了“因为它们实际上应该包含整个字符串。

On a completely different note, to make sure that you get the right data you should transform your CSV file into the following:

在完全不同的说明中,为了确保您获得正确的数据,您应该将CSV文件转换为以下内容:

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