我正在尝试从CSV文件中提取数据并生成http_build_query以作为http帖子提交
我的数据如下:
First,Last,Address,City,St,Zip,email,phone,dob,optindate,ipaddress,url
Abbey,Johnson,4004 S. Parker Dr. 206,Sioux Falls,SD,55106,abbey@email.com,6053451657,06/18/1924,4/19/2008 11:58:34,12.174.252.216,http://www.ecoupons.com/
我的代码如下所示:
<?PHP
$file_handle = fopen("test.2", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$data = array('firstname' => "$line_of_text[0]",
'lastname' => "$line_of_text[1]",
'address' => "$line_of_text[2]",);
echo http_build_query($data) . "\n";
}
fclose($file_handle);
?>
我的结果是:
firstname=Abbey&lastname=Johnson&address=4004+S.+Louise+Ave.+206
firstname=&lastname=&address=
我不确定为什么没有创建数据的第二行,以及如何在数组数据中保留空格?
谢谢!
答案 0 :(得分:0)
您的CSV似乎确实有效或可读(看不到新行)所以我使用的是我的简单
您的地址包含,
,这会影响fgetcsv
读取文件的方式
尝试
$fp= fopen("log.txt", "r");
while (!feof($fp) ) {
list($firstname,$lastname,$address) = fgetcsv($fp);
$data = array('firstname' => $firstname,
'lastname' => $lastname,
'address' => $address);
echo http_build_query($data) . PHP_EOL;
}
输出
firstname=Abbey&lastname=Johnso&address=4004+S.+Louise+Ave.+206