gpt4 book ai didi

PhP导出.csv将分隔符从逗号更改为其他

转载 作者:行者123 更新时间:2023-12-04 15:52:37 27 4
gpt4 key购买 nike

我有这个 PhP 代码,用户点击一个按钮,它会自动下载一个 .csv 文件,里面有不同类型的信息。所以这个 .csv 文件是逗号分隔的,所以这意味着只要有逗号,指针就会转到下一个单元格。但我想将分隔符更改为逗号以外的任何内容,因为我的某些数据包含逗号,例如位置列包含 501A、501B 等数据,并且它需要位于单个单元格中。但是它会转到另一个单元格,因此当我以纵向打印时 id 不适合。所以我只是想问如何更改分隔符并用逗号维护数据?谢谢!

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "envoy");
function db_connect() {

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die("ERROR!");
mysqli_select_db($db, DB_NAME);

return $db;
}

$db = db_connect();
date_default_timezone_set("EST5EDT");
$date = date('m-d-Y,h.i.sa');
$date2 = date('m/d/Y');
$filename = $date.'.csv';
//$fp = fopen($filename,"w");

$sql2 = "SELECT sku as Sku,name as 'Brochure Name',location as Location FROM brochureinfo where modified LIKE '$date2' ORDER BY name ";
$rs2 = mysqli_query($db, $sql2);
$total_records = mysqli_num_rows($rs2);
mysqli_close($db);

if($total_records>0){
$row = mysqli_fetch_assoc($rs2);

$seperator = "";
$comma = "";

foreach ($row as $name => $value){
$seperator.= $comma. ''.str_replace('','""',$name);
$comma=",";

}
$seperator .= "\n";

//echo $seperator;




mysqli_data_seek($rs2,0);

while ($row = mysqli_fetch_assoc($rs2)){


//$seperator = "";
$comma = "";

foreach ($row as $name => $value){

$value = str_replace( array( "\r" , "\n", "\r\n", "\n\r" ) ,'' , $value);
$value = str_replace('</b><br>','',$value);
$value = str_replace('<b>','',$value);
$value = str_replace('<br>','',$value);
$value = str_replace('<br />','',$value);

$seperator.= $comma. ''.str_replace('','""',$value);
$comma=",";

}
$seperator .= "\n";
}

header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');

echo $seperator;
}
else{
header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');
echo "No record modified today";}

最佳答案

您可以使用 fputcsv()用于生成 csv 格式的数据

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )

编辑:

你的代码看起来像这样:
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "envoy");
function db_connect() {

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die("ERROR!");
mysqli_select_db($db, DB_NAME);

return $db;
}

$db = db_connect();
date_default_timezone_set("EST5EDT");
$date = date('m-d-Y,h.i.sa');
$date2 = date('m/d/Y');
$filename = $date.'.csv';
$fp = fopen($filename,"w");

$sql2 = "SELECT sku as Sku,name as 'Brochure Name',location as Location FROM brochureinfo where modified LIKE '$date2' ORDER BY name ";
$rs2 = mysqli_query($db, $sql2);
$total_records = mysqli_num_rows($rs2);
mysqli_close($db);

if ($total_records>0) {
$row = mysqli_fetch_assoc($rs2);

$delimiter = ';';
$data = [];
foreach ($row as $name => $value){
$data[] = $name;
}
fputcsv($fp, $data, $delimiter);


mysqli_data_seek($rs2,0);

while ($row = mysqli_fetch_assoc($rs2)){
fputcsv($fp, $row, $delimiter);
}

fclose($fp);

header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');

echo $seperator;
}
else {
fclose($fp);

header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');
echo "No record modified today";
}

关于PhP导出.csv将分隔符从逗号更改为其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34164914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com