簡體   English   中英

如何刪除SQL數據末尾的Enter鍵並使用PHP將其導出到Excel

[英]How to remove enter key at the end of SQL data and export it to Excel using PHP

我正在嘗試將SQL數據導出到EXCEL,但是某些數據的末尾有一些回車鍵,因此當將其寫入excel數據時,它位於輸入鍵數據的右側,它將被寫入到下一行應該放置的位置旁

我試圖使用substr(),但不確定如何使用它,因為我是PHP新手

 <?php function leadZero($element) { return substr(trim($element),0,1) === "0" ? "'".$element : $element; } header("Content-Disposition: attachment; filename=\\"$filename\\""); header("Content-Type: application/vnd.ms-excel"); $user_query = mysqli_query($conn,$sql); //echo $user_query; // Write data to file $flag = false; while ($row = mysqli_fetch_assoc($user_query)) { if (!$flag) { // display field/column names as first row echo implode("\\t", array_keys($row)) . "\\r\\n"; $flag = true; } echo implode("\\t", array_map('leadZero', $row))."\\r\\n"; } ?> 

嘗試此操作以刪除換行符:

$string = trim(preg_replace('/\s\s+/', ' ', $string));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM