CSV 上传后将逗号分隔的字符串转换为两个字符串 [PHP]
Convert comma separated string into two strings after CSV upload [PHP]
我有一个脚本可以上传 csv 并将值分配给以逗号分隔的字符串
$has_title_row = true;
if( $_POST['upload_file'] == 1 ) {
if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
$filename = basename($_FILES['csvfile']['name']);
if(substr($filename, -3) == 'csv'){
$tmpfile = $_FILES['csvfile']['tmp_name'];
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
$i = 0;
while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {
if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
$i++;
continue;
}
//$data = print_r($items);
$i++;
$num = count($items);
$row++;
$str = '';
for ($c=0; $c < $num; $c++) {
//echo $items[$c] . ", ";
$str .= $items[$c] . ", ";
}
}
}
}
else{
die('Invalid file format uploaded. Please upload CSV.');
}
}
else{
die('Please upload a CSV file.');
}
}
在我上传的 csv 中,我有 2 列城市和国家
我还要删除带有标题的第一行。所以在 $str 中我有类似
的东西
$str = "Munich, Germany, Berlin, Germany, London, UK, Paris, France, Vienna, Austria, Milano, Italy, Rome, Italy";
我想要的结果是
$city = "Munich, Berlin, London, Paris, Vienna, Milano, Rome";
$country = "Germany, Germany, UK, France, Austria, Italy, Italy";
我如何将 $str 分成国家和城市,或者应该在我循环遍历结果的上传脚本中完成?
可以迭代数组,Demo
$str = "Munich, Germany, Berlin, Germany, London, UK, Paris, France, Vienna, Austria, Milano, Italy, Rome, Italy";
$array = explode(",",$str);
foreach($array as $k => $value){
if($k % 2){
$country_list[] = $value;
}else{
$city_list[] = $value;
}
}
$city = join(",",$city_list);
$country = join(",",$country_list);
与其处理当前代码的结果,不如按照评论中的建议直接处理 CSV 文件中的数据(仅包括相关部分)...
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
// Skip header
$header = fgetcsv($fh);
$cities = [];
$countries = [];
while (($items = fgetcsv($fh)) !== FALSE) {
$cities[] = $items[0];
$countries[] = $items[1];
}
print_r(implode(",",$cities));
print_r(implode(",",$countries));
}
我有一个脚本可以上传 csv 并将值分配给以逗号分隔的字符串
$has_title_row = true;
if( $_POST['upload_file'] == 1 ) {
if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
$filename = basename($_FILES['csvfile']['name']);
if(substr($filename, -3) == 'csv'){
$tmpfile = $_FILES['csvfile']['tmp_name'];
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
$i = 0;
while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {
if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
$i++;
continue;
}
//$data = print_r($items);
$i++;
$num = count($items);
$row++;
$str = '';
for ($c=0; $c < $num; $c++) {
//echo $items[$c] . ", ";
$str .= $items[$c] . ", ";
}
}
}
}
else{
die('Invalid file format uploaded. Please upload CSV.');
}
}
else{
die('Please upload a CSV file.');
}
}
在我上传的 csv 中,我有 2 列城市和国家
我还要删除带有标题的第一行。所以在 $str 中我有类似
的东西$str = "Munich, Germany, Berlin, Germany, London, UK, Paris, France, Vienna, Austria, Milano, Italy, Rome, Italy";
我想要的结果是
$city = "Munich, Berlin, London, Paris, Vienna, Milano, Rome";
$country = "Germany, Germany, UK, France, Austria, Italy, Italy";
我如何将 $str 分成国家和城市,或者应该在我循环遍历结果的上传脚本中完成?
可以迭代数组,Demo
$str = "Munich, Germany, Berlin, Germany, London, UK, Paris, France, Vienna, Austria, Milano, Italy, Rome, Italy";
$array = explode(",",$str);
foreach($array as $k => $value){
if($k % 2){
$country_list[] = $value;
}else{
$city_list[] = $value;
}
}
$city = join(",",$city_list);
$country = join(",",$country_list);
与其处理当前代码的结果,不如按照评论中的建议直接处理 CSV 文件中的数据(仅包括相关部分)...
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
// Skip header
$header = fgetcsv($fh);
$cities = [];
$countries = [];
while (($items = fgetcsv($fh)) !== FALSE) {
$cities[] = $items[0];
$countries[] = $items[1];
}
print_r(implode(",",$cities));
print_r(implode(",",$countries));
}